#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
using namespace std;
int main(){
int n;
int result[10];
for (int m = 0; m < 10; m++){
result[m] = 0;
}
scanf("%d",&n);
for (int i = 0; i < n; i++){
int cntP = 0, cntA = 0, cntT = 0;
bool flag = true;
char a[101];
scanf("%s", a);
for (int j = 0; a[j]!='\0'; j++){
if (a[j] == 'P')
cntP++;
else if (a[j] == 'A')
cntA++;
else if (a[j] == 'T')
cntT++;
else
{
flag = false;
break;
}
}
if (flag = true && cntP == 1 && cntA != 0 && cntT == 1){
char * p = NULL;
char * t = NULL;
p = strchr(a, 'P');
t = strchr(a, 'T');
if ((p - a)*(t - p - 1) == strlen(t) - 1)
flag = true;
else
flag = false;
}
if (flag == true)
result[i] = 1;
}
for (int i = 0; i < n; i++){
if (result[i] == 1)
printf("YES");
else
printf("NO");
if (i != n - 1)
printf("\n");
}
system("pause");
return 0;
}
AC20分通过
注意:
int cntP = 0, cntA = 0, cntT = 0;
bool flag = true;
必须在for循环里面初始化,因为每一次for循环是执行的一次判断
借鉴了别人用到的 strchr函数
if ((p - a)*(t - p - 1) == strlen(t) - 1)
这一句可调试看执行过程