java - 卡蒂斯 - 密室

标签 java algorithm

我需要为我的一门类(class)解决以下问题:https://open.kattis.com/problems/secretchamber

我的解决方案似乎有效,但第三个测试用例失败了。我实现了问题描述给出的每条规则,并且它解决了两个示例测试用例没有问题。我已经无计可施了,我不知道我做错了什么。

我不会要求任何人以任何方式做我的作业,但任何有关我遗漏的内容的指示将不胜感激。

import java.io.*;
import java.util.*;

class secretchamber
{

public static void main(String[] args)
{
    Scanner stdin = new Scanner(System.in);
    int numTranslations = stdin.nextInt();
    int numPairs = stdin.nextInt();
    stdin.nextLine();

    ArrayList<Character> trans1 = new ArrayList<Character>(numTranslations);
    ArrayList<Character> trans2 = new ArrayList<Character>(numTranslations);

    for(int i = 0; i < numTranslations; i++)
    {
        String temp = stdin.nextLine();
        trans1.add(temp.charAt(0));
        trans2.add(temp.charAt(2));
    }

    for(int i = 0; i < numPairs; i++)
    {
        String temp = stdin.nextLine();
        String[] pair = temp.split("\\s+");
        char[] a = pair[0].toCharArray();
        char[] b = pair[1].toCharArray();

        if(translates(a, b, numTranslations, trans1, trans2))
        {
            System.out.println("yes");
        }

        else
        {
            System.out.println("no");
        }
    }
}

public static boolean translates(char[] a, char[] b, int numTranslations, ArrayList trans1, ArrayList trans2)
{
    //false if strings are unequal in length
    if(a.length != b.length)
    {
        return false;
    }

    //true if strings are the same
    else if(a == b)
    {
        return true;
    }

    else
    {
        for(int i = 0; i < a.length; i++)
        {
            //if current characters are the same, continue
            if(a[i] == b[i])
            {
                continue;
            }

            //if current index of a directly translates to index of b
            else if(trans1.indexOf(a[i]) == trans2.indexOf(b[i]))
            {
                continue;
            }

            //if one or both of the characters do not exist within the list of translations
            else if(!(trans1.contains(a[i]) && trans2.contains(b[i])))
            {
                return false;
            }

            else
            {
                continue;
            }
        }
    }

    return true;
}
}

最佳答案

你对这个问题的理解很陈旧。您需要再次阅读问题中的这一部分。

Two words match if they have the same length and if each letter of the first word can be turned into the corresponding letter of the second word by using the available translations zero or more times.

此输入可能会使您的输出变得陈旧。

输入

2 1
一个c
d b
AAAABC

预期输出

没有

您的输出

是的

以下是此问题的解决方案草图。 我是从icpc网站上得到的。 enter image description here

关于java - 卡蒂斯 - 密室,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49291765/

相关文章:

java - 接口(interface)和抽象类广告方法覆盖

java - NoClassDefFoundError Android 与 ActionBarActivity

algorithm - 带有预定义皇后的 N 皇后

java - 时间过期数据结构

java - 对零、一和二的数组进行排序

C++ STL 算法,如 'comm' 实用程序

java - Java语法中::的含义

java - 如何创建频率表(整数数组)

java - javascript中的AES加密和java中的解密

算法 - 在整数序列中找到第一个缺失的整数