“答案正确”是自动判题系统给出的最令人欢喜的回复。本题属于PAT的“答案正确”大派送 —— 只要读入的字符串满足下列条件,系统就输出“答案正确”,否则输出“答案错误”。
得到“答案正确”的条件是:
1. 字符串中必须仅有P, A, T这三种字符,不可以包含其它字符;
2. 任意形如 xPATx 的字符串都可以获得“答案正确”,其中 x 或者是空字符串,或者是仅由字母 A 组成的字符串;
3. 如果 aPbTc 是正确的,那么 aPbATca 也是正确的,其中 a, b, c 均或者是空字符串,或者是仅由字母 A 组成的字符串。
输入格式: 每个测试输入包含1个测试用例。第1行给出一个自然数n (<10),是需要检测的字符串个数。接下来每个字符串占一行,字符串长度不超过100,且不包含空格。
输出格式:每个字符串的检测结果占一行,如果该字符串可以获得“答案正确”,则输出YES,否则输出NO。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 100
int Scan(char * a);
int Check (char *a);
int main()
{
int num;
scanf("%d",&num);
char str[N+1];
for(int i=0;i<num;i++)
{
scanf("%s",str);
Check(str);
}
return 0;
}
int Check (char *str)
{
int length=strlen(str);//the length of the string
int precount=0,midcount=0,endcount=0,i=0;
if(length<3)
{
printf("NO\n");
return 0;
}
while(str[i]!='P'&&i<length)
{
if(str[i]=='A')//accumulate the number of A before P
precount++;
else
{
printf("NO\n");
return 0;
}
i++;
}
i++;
while(str[i]!='T'&&i<length)
{
if(str[i]=='A')//accumulate the number of A before P
midcount++;
else
{
printf("NO\n");
return 0;
}
i++;
}
i++;
while(str[i]!='\n'&&i<length)
{
if(str[i]=='A')//accumulate the number of A before P
endcount++;
else
{
printf("NO\n");
return 0;
}
i++;
}
if(precount*midcount==endcount&&midcount!=0)
{
printf("YES\n");
return 0;
}
else
{
printf("NO\n");
return 0;
}
}