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:
- String must have only
P
,A
,T
three characters can not contain other characters; - Arbitrary shape such as
xPATx
a string can get " correct answer ", whereinx
either the empty string, or only by the letterA
string thereof; - If you
aPbTc
are correct, thenaPbATca
it is correct, whicha
,b
,c
are either empty string, or only by the letterA
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;
}