PAT judges correctly:
"The correct answer" is the most delightful response given by the automatic question-solving system. This question belongs to PAT's "correct answer" big delivery - as long as the read string satisfies the following conditions, the system will output "correct answer", otherwise output "wrong answer".
The conditions for obtaining the "correct answer" are:
(1) There must be only three characters in the string, P, A and T, and no other characters can be included.
(2) Any string like xPATx can get the "correct answer", where x is either an empty string or a string consisting only of the letter A;
(3) If aPbTc is correct, aPbATca is also correct, where a, b and c are either empty strings or strings consisting only of the letter A.
Now please write an automatic referee program for PAT to determine which strings can get the "correct answer".
Input format:
Each test input contains one test case. Line 1 gives a positive integer n (< 10), which is the number of strings to be detected. Next, each string takes up one line, and the length of the string does not exceed 100 and does not contain spaces.
Output format:
The detection result of each string takes up one line. If the string can get "correct answer", then output YES, otherwise output NO.
Input sample:
8
PAT
PAAT
AAPATAA
AAPAATAAAA
xPATx
PT
Whatever
APAAATAA
Output sample:
YES
YES
YES
YES
NO
NO
NO
NO
Code:
import java.util.Scanner; public class PatJudge { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int length = Integer.parseInt(sc.nextLine()); String temp[] = new String[length]; for (int i = 0; i < temp.length; i++) { temp[i] = sc.nextLine(); } for (int i = 0; i < temp.length; i++) { boolean isPAT = false; int pCounts = 0, aCounts = 0, tCounts = 0, otherCounts = 0; for (int j = 0; j < temp[i].length(); j++) { if ("p".equalsIgnoreCase(temp[i].charAt(j) + "")) { pCounts++; continue; } else if ("a".equalsIgnoreCase(temp[i].charAt(j) + "")) { aCounts++; continue; } else if ("t".equalsIgnoreCase(temp[i].charAt(j) + "")) { tCounts++; continue; } else { otherCounts++; continue; } } if (pCounts == 1 && aCounts > 0 && tCounts == 1 && otherCounts == 0 && length < 10 && temp[i].length() <= 100) { int lP = 0; // int lA = 0; int lT = 0; for (int k = 0; k < temp[i].length(); k++) { if ("p".equalsIgnoreCase(temp[i].charAt(k) + "")) { lP = k; } // if ("a".equalsIgnoreCase(temp[i].charAt(k) + "")) { // lA = k; // } if ("t".equalsIgnoreCase(temp[i].charAt(k) + "")) { lT = k; } } // if (lP == -1 || lA == -1 || lT == -1) { // isPAT = false; // } if (lP * (lT - lP - 1) == (temp[i].length() - lT - 1)) { isPAT = true; } } if (isPAT) { System.out.println("YES"); isPAT = false; } else { System.out.println("NO"); } } sc.close(); } }