“答案正确”是自动判题系统给出的最令人欢喜的回复。本题属于 PAT 的“答案正确”大派送 —— 只要读入的字符串满足下列条件,系统就输出“答案正确”,否则输出“答案错误”。
得到“答案正确”的条件是:
- 字符串中必须仅有
P、A、T这三种字符,不可以包含其它字符; - 任意形如
xPATx的字符串都可以获得“答案正确”,其中x或者是空字符串,或者是仅由字母A组成的字符串; - 如果
aPbTc是正确的,那么aPbATca也是正确的,其中a、b、c均或者是空字符串,或者是仅由字母A组成的字符串。
现在就请你为 PAT 写一个自动裁判程序,判定哪些字符串是可以获得“答案正确”的。
输入格式:
每个测试输入包含 1 个测试用例。第 1 行给出一个正整数 n (<10),是需要检测的字符串个数。接下来每个字符串占一行,字符串长度不超过 100,且不包含空格。
输出格式:
每个字符串的检测结果占一行,如果该字符串可以获得“答案正确”,则输出 YES,否则输出 NO。
输入样例:
8PATPAATAAPATAAAAPAATAAAAxPATxPTWhateverAPAAATAA
输出样例:
YESYESYESYESNONONONO
代码
这题还没整明白,0号和3号测试点没通过。。。。
#include<cstdio>#include<cstring>bool judge(char* input) {bool flagP, flagT, flagA;for(int i = 0; i < strlen(input); i++) {if(input[i] == 'P')flagP = true;else if(input[i] == 'T')flagT = true;else if(input[i] == 'A')flagA = true;elsereturn false;}// Make sure the characters only contain 'P' 'T' 'A'if(flagP && flagT && flagA) {// Find the position of 'P' and 'T'int positionT, positionP;for(int i = 0; i < strlen(input); i++) {if(input[i] == 'P') {positionP = i;break;}}for(int i = strlen(input) - 1; i >= 0; i--) {if(input[i] == 'T') {positionT = i;break;}}// If something else hides between 'P' and 'T'for(int i = positionP + 1; i < positionT; i++) {if(input[i] != 'A') {return false;}}int countAbeforeP = positionP;int countAbetweenPT = positionT - positionP - 1;int countAafterT = strlen(input) - positionT - 1;if(countAbeforeP * countAbetweenPT == countAafterT) {return true;}return false;}return false;}int main() {int number;scanf("%d", &number);char input[101];while(number) {scanf("%s", input);if(judge(input)) {printf("YES\n");}else {printf("NO\n");}number--;}return 0;}
