By 1003 I want to! (20 points)

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/chen_zan_yu_/article/details/102689439

By 1003 I want to! (20 points)

" 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 <bits/stdc++.h>
using namespace std;

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        string s;
        cin>>s;
        int len=s.size();
        int num_p=0,num_t=0,other=0;
        int loc_p=-1,loc_t=-1;
        for(int i=0; i<len; i++)
        {
            if(s[i]=='P')
            {
                num_p++;
                loc_p=i;
            }
            else if(s[i]=='T')
            {
                num_t++;
                loc_t=i;
            }
            else if(s[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;
        int y=loc_t-loc_p-1;
        int z=len-loc_t-1;
        if(z-x*(y-1)==x)
        {
            printf("YES\n");
        }
        else
        {
            printf("NO\n");
        }

    }

    return 0;
}

 

Guess you like

Origin blog.csdn.net/chen_zan_yu_/article/details/102689439