1003 我要通过! (20 分)
“答案正确”是自动判题系统给出的最令人欢喜的回复。本题属于 PAT 的“答案正确”大派送 —— 只要读入的字符串满足下列条件,系统就输出“答案正确”,否则输出“答案错误”。
得到“答案正确”的条件是:
P
、 A
、 T
这三种字符,不可以包含其它字符;xPATx
的字符串都可以获得“答案正确”,其中 x
或者是空字符串,或者是仅由字母 A
组成的字符串;aPbTc
是正确的,那么 aPbATca
也是正确的,其中 a
、 b
、 c
均或者是空字符串,或者是仅由字母 A
组成的字符串。现在就请你为 PAT 写一个自动裁判程序,判定哪些字符串是可以获得“答案正确”的。
每个测试输入包含 1 个测试用例。第 1 行给出一个正整数 n (<),是需要检测的字符串个数。接下来每个字符串占一行,字符串长度不超过 100,且不包含空格。
每个字符串的检测结果占一行,如果该字符串可以获得“答案正确”,则输出 YES
,否则输出 NO
。
8
PAT
PAAT
AAPATAA
AAPAATAAAA
xPATx
PT
Whatever
APAAATAA
YES
YES
YES
YES
NO
NO
NO
NO
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in =new Scanner(System.in); int num =in.nextInt(); String []result=new String[num]; for(int i =0;i<num;i++) { String testStr=in.next(); result[i]=myMethod(testStr); } for(String s:result) { System.out.println(s); } } private static String myMethod(String s) { // TODO Auto-generated method stub char[]tempChar=s.toCharArray(); int countP=0; int countA=0; int countT=0; String result=""; for (int i =0;i<tempChar.length;i++) { if(tempChar[i]!=‘P‘&&tempChar[i]!=‘A‘&&tempChar[i]!=‘T‘) { break; } else if(tempChar[i]==‘P‘) { countP++; } else if(tempChar[i]==‘A‘) { countA++; } else if(tempChar[i]==‘T‘) { countT++; } if(countP==2||countT==2) { break; } } if(countP==1&&countT==1&&countA!=0) { int indexP=s.indexOf(‘P‘); int indexA=s.indexOf(‘A‘); int indexT=s.indexOf(‘T‘); if((indexP)*(indexT-indexP-1)==(s.length()-indexT-1)) { result="YES"; } else { result="NO"; } } else { result="NO"; } return result; } }
原文:https://www.cnblogs.com/xiaozhushifu/p/10845780.html