1003. 我要通过!(20)
时间限制
400 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
CHEN, Yue
“答案正确”是自动判题系统给出的最令人欢喜的回复。本题属于PAT的“答案正确”大派送 —— 只要读入的字符串满足下列条件,系统就输出“答案正确”,否则输出“答案错误”。
得到“答案正确”的条件是:
1. 字符串中必须仅有P, A, T这三种字符,不可以包含其它字符;
2. 任意形如 xPATx 的字符串都可以获得“答案正确”,其中 x 或者是空字符串,或者是仅由字母 A 组成的字符串;
3. 如果 aPbTc 是正确的,那么 aPbATca 也是正确的,其中 a, b, c 均或者是空字符串,或者是仅由字母 A 组成的字符串。
输入格式: 每个测试输入包含1个测试用例。第1行给出一个自然数n (<10),是需要检测的字符串个数。接下来每个字符串占一行,字符串长度不超过100,且不包含空格。
输出格式:每个字符串的检测结果占一行,如果该字符串可以获得“答案正确”,则输出YES,否则输出NO。
输入样例:8 PAT PAAT AAPATAA AAPAATAAAA xPATx PT Whatever APAAATAA输出样例:
YES YES YES YES NO NO NONO
package Basic1003; import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Main { public static void main(String[] args) { // 第一种输入方法 // 这种输入方法不行,两个Scanner,编译能通过,但是提交的结果不对,要用第二种 /* * Scanner intIn = new Scanner(System.in); int num = intIn.nextInt(); * * List<String> list = new ArrayList<String>(); while (num > 0) { * Scanner stringIn = new Scanner(System.in); String str = * stringIn.nextLine(); list.add(str); num--; } for (String x : list) { * System.out.println(judge(x)); } */ // 第二种输入方法 int flagSum = 0; ArrayList<String> stringList = new ArrayList<String>(); Scanner sc = new Scanner(System.in); flagSum = Integer.parseInt(sc.nextLine()); for (int i = 0; i < flagSum; ++i) { stringList.add(sc.nextLine()); } // 判断 for (String x : stringList) { judge(x); } } // 第一种判断方法 // 这种方法不行,return的字符串打印出来以后是对的,但是提交的时候显示不对,要用第二种 /* * public static String judge(String s) { int flag = 1; String pattern = * "A*PA+TA*"; String pattern1 = "PA+T"; if (s.matches(pattern)) { if * (s.matches(pattern1)) { flag = 1; } else { String temp[] = * s.split("P|T"); int aLength = temp[0].length(); int bLength = * temp[1].length(); int cLength = temp[2].length(); if ((cLength - aLength) * / aLength == (bLength - 1)) { flag = 1; } else { flag = 0; } } } else { * flag = 0; } * * if (flag == 1) { return "YES"; } else { return "NO"; } } */ // 第二种判断方法 public static void judge(String s) { String pattern = "A*PA+TA*"; String pattern1 = "PA+T"; if (s.matches(pattern)) { if (s.matches(pattern1)) { System.out.println("YES"); } else { String temp[] = s.split("P|T"); int aLength = temp[0].length(); int bLength = temp[1].length(); int cLength = temp[2].length(); if ((cLength - aLength) / aLength == (bLength - 1)) { System.out.println("YES"); } else { System.out.println("NO"); } } } else { System.out.println("NO"); } } }