1003. I want to pass (PAT), 1003 I want to pass pat
The question stem is as follows:
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 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.
What needs to be done now is to consider which forms of strings are the correct form. First, check condition 1. The string contains only three types of characters: P, A, and T. This means that if other characters appear, the string will certainly not meet the conditions. Then, let's look at condition 2. the string of any XPATX meets the condition. In this case, the two ends of PAT are either empty characters or equal numbers of. The final key is Condition 3. If aPbTc is correct, aPbATca is also correct. Obviously, string B cannot be empty, and if string a is not equal to string c, aPATc is incorrect (condition 2 is not met ). So let's assume that string B is "A" and string a is equal to string c, then aPATa is correct, and then aPAAT (aa) is also correct. Continue to derivation, aPAAT (aa) yes, so aPAAAT (aaa) is correct, aPAAAT (aaa) is correct, and aPAAAAT (aaaa) is also correct ...... it can be concluded that the string in the form of aP (n A) T (n a) is correct.
To sum up, the string requirements: aP (n A) T (n a), where string a is null or A string, and n is an integer greater than or equal to 0.
1 # include <iostream> 2 using namespace std; 3 int juge (char * s); 4 int main () {5 int n, I; 6 char s [10] [105]; 7 cin> n; 8 for (I = 0; I <n; I ++) 9 cin> s [I]; 10 for (I = 0; I <n; I ++) {11 if (juge (s [I]) 12 printf ("YES \ n "); 13 else printf ("NO \ n"); 14} 15 return 0; 16} 17 int juge (char * s) {18 int I, lena = 0, lenb = 0, len; 19 for (I = 0; I ++) {20 if (s [I] = 'A ') // The number of a in string A 21 lena ++; 22 else {23 if (s [I] = 'P') 24 break; 25 else return 0; 26} 27} 28 for (I = I + 1; I ++) {29 if (s [I] = 'A ') // The number of A in string B is 30 lenb ++; 31 else {32 if (s [I] = 'T') 33 break; 34 else return 0; 35} 36} 37 if (lenb = 0) return 0; // B is an empty string 38 len = I + lena * lenb + 1; 39 for (I = I + 1; I <len; I ++) {// whether the ac string is A 40 if (s [I] = 'A') 41 continue; 42 else return 0; 43} 44 if (s [len] = '\ 0') return 1; // whether to reach 45 else return 0; 46} // AC
Link: https://www.patest.cn/contests/pat-b-practise/1003