这题挺有意思,题目不太好理解,要仔细思考思考,P和T之间至少有一个A,而每多一个A,T后面A的数量就得加上P前A的数量,在初始时P和T左右两边A的数量又必须相等,所以可以得出T右侧A的数量=P左侧A的数量*P与T之间A的数量。
#include<stdio.h>
int main(){
int n[3]={0};
int check[10]={0};
int pp=0,tp=0,position=0;
int f=1;
int num,count=1;
char ch;
scanf("%d",&num);
ch=getchar();
while(count<=num){
while((ch=getchar())!='\n'){
if(ch!='P' && ch!='A' && ch!='T'){
f=0;
}else if(ch=='P'){
if(pp==0){
pp=1;
position=1;
}else{
f=0;
}
}else if(ch=='T'){
if(tp==0 && pp==1){
tp=1;
position=2;
}else{
f=0;
}
}else{
n[position]++;
}
}if(f==1 && n[2]==n[0]*n[1] && n[1]!=0 && tp==1){
check[count]=1;
}else{
check[count]=0;
}count++;
f=1;
n[0]=0;n[1]=0;n[2]=0;
pp=0;tp=0;position=0;
}
for(int i=1; i<=num; i++){
if(check[i]==1){
printf("YES\n");
}else{
printf("NO\n");
}
}
return 0;
}
注意别忘特殊情况,PA,AT,脑子清楚题就不难。