1003. I want to pass! (20)
" Correct answer " is the most pleasing response from the automated question answering system. This question belongs to PAT's " correct answer " big distribution - as long as the read string meets the following conditions, the system will output " correct answer ", otherwise it will output " wrong answer ".
The conditions for getting " correct answer " are:
1. There must be only three characters of P, A, T in the string, and cannot contain other characters;
2. Any string in the form of xPATx can get " correct answer ", where x is either an empty string, or is a string consisting only of the letter A;
3. If aPbTc is true, then aPbATca is also true, where a, b, c are either an empty string, or a string consisting only of the letter A.
Input format: Each test input contains 1 test case. Line 1 gives a natural number n (<10), which is the number of strings to be detected. Next, each string occupies 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 occupies one line, if the string can get " correct answer ", output YES, otherwise output NO.
Input sample:8
PAT
PAAT
AAPATAA
AAPAATAAAA
xPATx
PT
Whatever
APAAATAA
Sample output:
YES
YES
YES
YES
NO
NO
NO
NO
-------------------------------------------------- --Dividing line---------------------------------------------- --------------
Idea: Condition 1 must be satisfied, and it is easy to write. The emphasis is on the understanding of the second and third conditions.
Each string that meets condition 2 can continue to push out new strings according to condition 3, and the pushed strings can continue to push.
So you can try to list a few and find a pattern.
PAT This string meets condition 2, then the string derived from condition 3 is PAAT, and then continues to push PAAAT, that is:
PAT ---> PAAT ---> PAAAT ---> PAAAAT
APATA ---> APAATAA ---> APAAATAAA ---> APAAAATAAAA
AAPATAA ---> AAPAATAAAA ----> AAPAAATAAAAAA ----> AAPAAAATAAAAAAA
It can be seen that the values of a, b, and c in the qualified string aPbTc will always satisfy the relationship a * b = c.
import java.util.Scanner; public class PAT_B_1003//The class name needs to be changed to Main when submitting on the PAT platform { public static void main(String[] agrs) { Scanner in = new Scanner(System.in);//Read all input int n = in.nextInt();//Read the first number of the input, which represents the number of samples input later in.nextLine();//Enter key after clearing numbers for(int i = 0; i < n; i++)//judgment for each example { String str = in.nextLine();//Input sample string if(onlyPAT(str))//If there are only three characters of PAT, then judge whether the second and third conditions are met { if(aPbTc(str)) { yes();//In line with the output YES } else { no();//Does not match the output NO } } else//It contains more than three characters of PAT, output NO { no(); } } } public static void yes()//输出YES { System.out.println("YES"); } public static void no () // export NO { System.out.println("NO"); } public static boolean onlyPAT(String str) {//Traverse the string once, return false if there are not PAT characters for(int i = 0; i < str.length(); i++) { if(str.charAt(i) != 'P' && str.charAt(i) != 'A' && str.charAt(i) != 'T') { return false; } } return true; } public static boolean aPbTc(String str) { int a = str.indexOf("P");//aPbTc string, the value of a represents the number of A before the character P int c = str.length() - str.indexOf("T") - 1;//aPbTc In the string, the value of c represents the number of A after the character T int b = str.indexOf("T") - str.indexOf("P") - 1;//aPbTc string, the value of b represents the number of A between P and T if((b == 0 ) || (a * b != c))//It can be seen from the example that b cannot be 0 return false; else return true; } }