PAT 乙级1003. 我要通过!(20)

答案正确”是自动判题系统给出的最令人欢喜的回复。本题属于PAT的“答案正确”大派送 —— 只要读入的字符串满足下列条件,系统就输出“答案正确”,否则输出“答案错误”。

得到“答案正确”的条件是:

1. 字符串中必须仅有P, A, T这三种字符,不可以包含其它字符;
2. 任意形如 xPATx 的字符串都可以获得“答案正确”,其中 x 或者是空字符串,或者是仅由字母 A 组成的字符串;
3. 如果 aPbTc 是正确的,那么 aPbATca 也是正确的,其中 a, b, c 均或者是空字符串,或者是仅由字母 A 组成的字符串。

现在就请你为PAT写一个自动裁判程序,判定哪些字符串是可以获得“答案正确”的。

输入格式: 每个测试输入包含1个测试用例。第1行给出一个自然数n (<10),是需要检测的字符串个数。接下来每个字符串占一行,字符串长度不超过100,且不包含空格。

输出格式:每个字符串的检测结果占一行,如果该字符串可以获得“答案正确”,则输出YES,否则输出NO。

输入样例:

8
PAT
PAAT
AAPATAA
AAPAATAAAA
xPATx
PT
Whatever
APAAATAA

输出样例:

YES
YES
YES
YES
NO
NO
NO
NO

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cmath>
 4 #include<cstring>
 5 using namespace std;
 6 
 7 
 8 int main()
 9 {
10     int n;
11     int nump,numt,numa;
12     int posp,post;
13     char ch[105];
14     scanf("%d",&n);
15     while(n--)
16     {
17         cin>>ch;
18         nump=0;numt=0;numa=0;
19         int len=strlen(ch);
20         for(int i=0;i<len;i++)
21         {
22             if(ch[i]=='P')
23             {
24                 nump++;
25                 posp=i;
26             }
27             else if(ch[i]=='T')
28             {
29                 numt++;
30                 post=i;
31             }
32             else if(ch[i]=='A')
33                 numa++;
34         }
35         if((nump!=1)||(numt!=1)||(numa!=len-2)||(post<posp)||numa==0)
36         {
37             printf("NO
");
38             continue;
39         }
40         if(len-1-post-posp!=(post-posp-2)*(posp))
41             printf("NO
");
42         else
43             printf("YES
");
44     }
45     return 0;;
46 
47 }
原文地址:https://www.cnblogs.com/Annetree/p/7263231.html