【PAT】1003. I want to pass! (20)

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 correct, then aPbATca is also correct, where a, b, c are either an empty string, or a string 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 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
In the NO 

question, A is a special character, because only the number of A is variable.
Observe the third condition in the title: "If aPbTc is correct, then aPbATca is also correct" (a, b, c are strings containing only A or empty strings),
you can find that an A is added between P and T , and a is added after T, there is a linear increase relationship: a*1=a, that is:
  the number of A before P × the number of A between P and T = the number of A after T
Condition 2 is A case of the above formula.
According to the three conditions given in the question, it can be concluded that the conditions that the string that can obtain the "correct answer" should meet are:
  1. Only contain three characters of P, A and T 2. There
  is only one number of P and T respectively, And P is in front of T
  3. Satisfy the formula: the number of A before P × the number of A between P and T = the number of A after T
For example: APT output "YES", ATP output "NO"

C code is as follows:
1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include < string .h>
 4  
5  int main(){
 6      int n,i,j;
 7      int len;   // store each string Length of 
8      int num_p,num_t,num_a;   // Store the number of P, T, A 
9      int pos_p,pos_t;   // Store P, T position 
10      char str_input[ 10 ][ 100 ];
 11      scanf( " % d\n " ,& n);
 12      for(i=0;i<n;i++){
13         scanf("%s\n",&str_input[i]);        
14     }
15     for(i=0;i<n;i++){
16         len=strlen(str_input[i]);
17         num_p=num_t=num_a=pos_p=pos_t=0;
18         for(j=0;j<len;j++){
19             if(str_input[i][j]=='P'){
20                 num_p++;
21                 pos_p=j;
 22                  if (num_p> 1 ) break ;   // If the number of 'P' is greater than 1, jump out of the loop and output NO 
23              }
 24              else  if (str_input[i][j]== ' T ' ){
 25                  num_t++ ;
 26                  pos_t = j;
 27                  if (num_t> 1 ) break ; // if the number of 'T' is greater than 1, jump out of the loop and output NO 
28              }
 29              else  if (str_input[i][j]== ' A ' ){
 30                 num_a++ ;
 31              }
 32              else  break ;   // Include other characters, exit the loop, output NO 
33          }
 34          if ((pos_p*(pos_t-pos_p- 1 )==len-pos_t- 1 ) && num_p== 1 && num_t= = 1 && num_a> 0 )
 35              printf( " YES\n " );
 36          else printf( " NO\n " );        
 37      }
 38      system( " pause ");    
39     return 0;
40 }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324739495&siteId=291194637
Recommended