题目注意点
- 正确的字符串中P和T都只有一个
- 题目的条件2才是最基本的判定条件,条件3要由条件2推出
实现
#include <stdio.h>
#include <string.h>
int main(){
int n;
char str[105];
scanf("%d", &n);
while (n--) {
char str[105];
scanf("%s", str);
int len = strlen(str);
int numP = 0, numT = 0, other = 0;
int locP = -1, locT = -1;
for (int i = 0; i < len; i++) {
if (str[i] == 'P') {
numP++;
locP = i;
} else if (str[i] == 'T') {
numT++;
locT = i;
} else if (str[i] != 'A') {
other++;
}
}
if ((numP != 1) || (numT != 1) || (other) || locP + locT <= 1) {
printf("NO\n");
continue;
}
int x = locP, y = locT - locP - 1, z = len - locT - 1;
if (x * y == z) printf("YES\n");
else printf("NO\n");
}
return 0;
}