Link: http://pat.zju.edu.cn/contests/pat-b-practise/1003
"Correct answer"Is the most gratifying response provided by the automatic question determination system. This question belongs to Pat's"Correct answer"Big delivery-as long as the string to be read meets the following conditions, the system outputs"Correct answer"; Otherwise,"Incorrect answer".
Get"Correct answerThe condition is:
1. The string must contain only three characters: P, A, and T. It cannot contain other characters;
2. Any string such as xpatx can obtain"Correct answer", Where X is either a null string or a string consisting of only letters;
3. If apbtc is correct, apbatca is correct, where A, B, and C are either empty strings or strings consisting of only letters.
Now, please write an automatic referee program for Pat to determine which strings can be obtained
Correct answer.
Input Format:Each test input contains one test case. Row 1st provides a natural number N (<10), which is the number of strings to be checked. Next, each string occupies one row. The length of the string cannot exceed 100 and does not contain spaces.
Output Format:The detection result of each string occupies one line. If this string can be obtained,Correct answer", The output is yes; otherwise, the output is no.
Input example:
8PATPAATAAPATAAAAPAATAAAAxPATxPTWhateverAPAAATAA
Output example:
YESYESYESYESNONONONO
Ideas:
The number of A before P multiplied by the number of A between P and T is equal to the number of A after P.
For the 1st rules: 0*0 = 0.
For the 2nd rules: x * 1 = x.
For the first rule: if apbtc is correct, B must be a, a = C, BA is AA, A * 2 = AA = Ca.
The Code is as follows:
# Include <cstdio> # include <cstring> # include <algorithm> # include <iostream> # include <string> using namespace STD; int judge (string S) {// determine whether the for (INT I = 0; I <S. size (); I ++) {If (s [I]! = 'P' & S [I]! = 'A' & S [I]! = 'T') {return 1 ;}} return 0 ;}int main () {string s; int t; scanf ("% d", & T ); while (t --) {CIN> S; int flag = judge (s); If (FLAG) {printf ("NO \ n"); continue ;} int contp = 0, contt = 0; int posp = 0, post = 0; For (INT I = 0; I <S. size (); I ++) {// check the number and position of P and T if (s [I] = 'P') {contp ++; posp = I;} else if (s [I] = 'T') {contt ++; post = I ;}} if (contp! = 1 | contt! = 1) {// The number of P and T is more than one, and the error printf ("NO \ n"); continue;} else if (post-posp <2) {// P, T. If no other letter (a) exists between them, the printf ("NO \ n"); continue;} int I = 0; // multiply the number of A on the left of P by the number in the middle that is equal to the number on the Right of t int cleft = 0, cmid = 0, cright = 0; for (I = 0; I <S. size (); I ++) {If (s [I] = 'P') break; else if (s [I] = 'A ') cleft ++;} For (; I <S. size (); I ++) {If (s [I] = 'T') break; else if (s [I] = 'A ') cmid ++;} For (; I <S. size (); I ++) {If (s [I] = 'A') cright ++;} If (cleft * cmid = cright) printf ("Yes \ n"); else printf ("NO \ n");} return 0 ;}
1003. I want to pass! (20) (zjupat simulation)