" Right answer " is the most joy automatic reply sentence topic given system. This title belongs to the PAT of "correct 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, it must not contain other characters;
- Arbitrary shape such as
xPATx
a string can get "correct answer", whereinx
either the empty string, or a string consisting only of letters A; - If you
aPbTc
are correct, then aPbATca 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 output NO.
Sample input:
8
PAT
PAAT
AAPATAA
AAPAATAAAA
xPATx
PT
Whatever
APAAATAA
Sample output:
YES
YES
YES
YES
NO
NO
NO
NO
Analysis: conditions may answer given condition is not clear, but you can see the input and output sample according to the following: First, you want to correct P
and T
certainly have and the only one in PAT
can be inserted in any position A
, but which also have certain rules. Since then APAAATAA
is wrong, AAPATAA
is correct, the analysis for a long time, then looked at a sister school of the articles found, must be
P
before A
the number *
P
and T
the number of A between the =
T
following A
number of
detailed analysis of the following links:
poke here
After clarify this issue resolved just fine.
#include<stdio.h>
#include<string.h>
int main()
{
int n;
char str[101];
scanf("%d",&n);
int i,j;
for(i=0;i<n;i++){
int a=0;//记录A的数目
int p=0;//P的个数
int t=0;//T的个数
int lp,lt;//P和T的位置
int flag=1;//循环控制
scanf("%s",str);
for(j=0;str[j]!='\0';j++){
if(str[j]=='A'){
a++;
}
else if(str[j]=='T'){
t++;
lt=j;
}
else if (str[j]=='P'){
p++;
lp=j;
}
else {
flag=0;
break;
}
}
if(p==1 && t==1 && a!=0 && (lt-lp)!=1 && lp*(lt-lp-1)==strlen(str)-lt-1 ){
printf("YES\n");
}
else{
printf("NO\n");
}
}
return 0;
}