PAT level B Question 1003. I want to pass! (20) (Problem Solving), pat1003
"Correct answer"Is the most gratifying response provided by the automatic question determination system. This is PAT's "correct answer" big delivery-as long as the string to be read meets the following conditions, the system will output "correct answer", or "Wrong 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 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 obtainedCorrect 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 check result of each string occupies one line. If the string can obtain "correct answer", YES is output; otherwise, NO is output.
Input example: 8 PATPAATAAPATAAAAPAATAAAAxPATxPTWhateverAPAAATAA
Output example: Yesyesyesyesnonono
Code:
/********** Compiling environment ** vc ++ 6.0 (32-bit) * ************/# include <stdio. h> # include <string. h> # include <malloc. h> # define M 101int main () {int n, I, j; int cp = 0, ct = 0, ca = 0, flag = 0, ce = 0; char ** s; char * p1, * p2; scanf ("% d", & n); s = (char **) malloc (n * sizeof (char *); // defines the first column for (I = 0; I <n; I ++) {s [I] = (char *) malloc (M * sizeof (char); // defines the row scanf ("% s", s [I]); strcat (s [I], "\ 0") ;}for (I = 0; I <n; I ++) {cp = 0; ct = 0; ca = 0; ce = 0; for (j = 0; S [I] [j]! = '\ 0'; j ++) {if (s [I] [j] = 'P') cp ++; else if (s [I] [j] = 'T') ct ++; else if (s [I] [j] = 'A') ca ++; else ce = 1;} if (cp = 1 & ct = 1 & ca! = 0 & ce = 0) {p1 = strchr (s [I], 'P'); p2 = strchr (s [I], 'T '); if (p1-s [I]) * (p2-p1-1) = strlen (p2)-1) flag = 1;} else flag = 0; if (flag = 1) printf ("YES \ n"); else printf ("NO \ n");} return 0 ;}