5.1 Simple math: B1003 me through!

B1003 me through!

" Right answer " is the most joy automatic reply sentence topic given system. This title belongs to the PAT of " right answer " big delivery - just read the string following conditions are satisfied, the system will output " right answer ", otherwise outputs " Wrong Answer ."

Get the " right answer " conditions are:

  1. String must have only  PATthree characters can not contain other characters;
  2. Arbitrary shape such as  xPATx a string can get " correct answer ", wherein  x either the empty string, or only by the letter  A string thereof;
  3. If you  aPbTc are correct, then  aPbATca it is correct, which  abc are either empty string, or only by the letter  A string composed.

Now ask you to write a PAT referee program automatically determines which strings can get " correct answer " is.

Input formats:

Each test comprises a test input. Line 1 is given a positive integer n (<10), is the number of strings to be detected. Next, one row for each string, the string length of not more than 100, no spaces.

Output formats:

The detection result row for each string, if the string can get " correct answer ", the output  YES, otherwise the output  NO.

Sample input:

8
PAT
PAAT
AAPATAA
AAPAATAAAA
xPATx
PT
Whatever
APAAATAA

Sample output:

YES
YES
YES
YES
NO
NO
NO
NO

 


 


#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;

int main(){
    int n;
    scanf("%d",&n);
    while(n--){
        char str[110];
        scanf("%s",str);
        int len=strlen(str);
        int num_p=0,num_t=0,other=0;
        int loc_p=-1,loc_t=-1;
        for(int i=0;i<len;i++){
            if(str[i]=='P'){
                num_p++;
                loc_p=i;
            }
            else if(str[i]=='T'){
                num_t++;
                loc_t=i;
            }
            else if(str[i]!='A') other++;
        }
        if((num_p!=1)||(num_t!=1)||(other!=0)||(loc_t-loc_p<=1)){
            printf("NO\n");
            continue;
        }
        int x=loc_p,y=loc_t-loc_p-1,z=len-loc_t-1;
        if(z-x*(y-1) == x){
            printf("YES\n");
        }
        else
            printf("NO\n");
    }
    return 0;
}

 

Published 157 original articles · won praise 15 · views 10000 +

Guess you like

Origin blog.csdn.net/nanke_4869/article/details/104629209
5.1