我需要为我的一个课程解决以下问题: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;
}
}