浙大PAT试题 1003 我要通过!
牛客网20分通过,PAT平台18分,有一个测试点未通过,不知错误点,有待改进!
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int n = scan.nextInt(); String str[] = new String[n]; for (int i = 0; i < n; i++) { str[i] = scan.next(); } for (int i = 0; i < n; i++) { System.out.println(judgeStr(str[i])); } } public static String judgeStr(String str) { int Pflag = 0, Tflag = 0, Afirst = 0, Alast = 0, Amid = 0; char[] s = str.toCharArray(); for (int i = 0; i < s.length; i++) { if (s[i] != 'P' && s[i] != 'A' && s[i] != 'T') { break; } if (s[i] == 'P') { Pflag = 1; } if (s[i] == 'T') { Tflag = 1; } if (s[i] == 'A' && Pflag == 0) { Afirst++; } else if (s[i] == 'A' && Tflag == 1) { Alast++; } else if (s[i] == 'A' && Pflag == 1 && Tflag == 0) { Amid++; } } if (Amid != 0 && Pflag == 1 && Tflag == 1 && Afirst * Amid == Alast) { return "YES"; } else { return "NO"; } } }