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 NO NO
import java.util.Scanner; public class Main { public static void main(String[] args) { int n; Scanner sc = new Scanner(System.in); n = sc.nextInt(); sc.nextLine(); String[] s = new String[n]; for(int i = 0; i < n; i ++) { s[i] = sc.nextLine(); } for(int i = 0;i < n; i ++) { System.out.println(isPass(s[i])); } } private static String isPass(String s) { int p_pos = s.indexOf('P'); int t_pos = s.indexOf('T'); int cnt1 = p_pos; int cnt2 = t_pos - p_pos - 1; int cnt3 = s.length() - t_pos - 1; // System.out.print(" " + p_pos + t_pos + cnt1 + cnt2 + cnt3); if(cnt1 * cnt2 != cnt3 ) { //System.out.print("error1"); return "NO"; } char[] letter = s.toCharArray(); for(int i = 0; i < p_pos; i ++) { if(letter[i] != 'A'){ // System.out.print("error2"); return "NO"; } } for(int i = p_pos + 1; i < t_pos; i ++ ) { if(letter[i] != 'A'){ //System.out.print("error3"); return "NO"; } } for(int i = t_pos + 1; i < s.length(); i ++) { if(letter[i] != 'A') { // System.out.print("error4"); return "NO"; } } if(cnt2 == 0) return "NO"; return "YES"; } }