“答案正确”是自动判题系统给出的最令人欢喜的回复。本题属于 PAT 的“答案正确”大派送 —— 只要读入的字符串满足下列条件,系统就输出“答案正确”,否则输出“答案错误”。
得到“答案正确”的条件是:
字符串中必须仅有 P、 A、 T
这三种字符,不可以包含其它字符;
任意形如 xPATx
的字符串都可以获得“答案正确”,其中 x
或者是空字符串,或者是仅由字母 A
组成的字符串;
如果 aPbTc
是正确的,那么 aPbATca
也是正确的,其中 a、 b、 c
均或者是空字符串,或者是仅由字母 A
组成的字符串。
现在就请你为 PAT 写一个自动裁判程序,判定哪些字符串是可以获得“答案正确”的。
输入格式:
每个测试输入包含 1 个测试用例。第 1 行给出一个正整数 n (≤10),是需要检测的字符串个数。接下来每个字符串占一行,字符串长度不超过 100,且不包含空格。
输出格式:
每个字符串的检测结果占一行,如果该字符串可以获得“答案正确”,则输出 YES
,否则输出 NO
。
输入样例:
10
PAT
PAAT
AAPATAA
AAPAATAAAA
xPATx
PT
Whatever
APAAATAA
APT
APATTAA
输出样例:
YES
YES
YES
YES
NO
NO
NO
NO
NO
NO
#include <stdio.h>
#include <string.h>
#define MAXS 112
int main() {
int n, i, a, b, c, status, flag;
char pat[MAXS];
// Read the number of test cases
if (scanf("%d", &n) == EOF) {
printf("error\n");
return 1; // Exit with an error code
}
while (n--) {
a = b = c = status = flag = 0;
// Read the input pattern
if (scanf("%s", pat) == EOF) {
printf("error\n");
return 1; // Exit with an error code
}
// Check the pattern
for (i = 0; i < strlen(pat); ++i) {
if (pat[i] != 'P' && pat[i] != 'A' && pat[i] != 'T') {
// If a character other than 'P', 'A', or 'T' is encountered
if (flag = 1)
break; // Set flag and break out of the loop
} else {
switch (pat[i]) {
case 'P':
// Transition from status 0 to 1
if (status == 0) {
status = 1;
} else {
flag = 1;
}
break;
case 'T':
// Transition from status 1 to 2
if (status == 1) {
status = 2;
} else {
flag = 1;
}
break;
case 'A':
// Count occurrences based on the current status
switch (status) {
case 0:
++a;
break;
case 1:
++b;
break;
case 2:
++c;
break;
}
}
if (flag)
break; // Set flag and break out of the loop
}
}
// Check if the pattern follows the specified conditions
(flag == 0 && status == 2 && b != 0 && a * b == c) ? printf("YES\n") : printf("NO\n");
}
return 0; // Exit with success
}