B1003. 我要通过!(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 <stdio.h>
 2 #include <stdlib.h>
 3 #include <iostream>
 4 #include <string.h>
 5 #include <string>
 6 #include <math.h>
 7 #include <algorithm>
 8 using namespace std;
 9 
10 
11 int main(){
12     int t;
13     scanf("%d",&t);
14     while(t--)
15     {
16         char str[110];
17         scanf("%s",str);
18         int len=strlen(str);
19         int num_p=0,num_t=0,other=0;
20         int loc_p=-1,loc_t=-1;
21         for(int i=0;i<len;i++)
22         {
23             if(str[i]=='P')
24             {
25                 num_p++;
26                 loc_p=i;
27             }else if(str[i]=='T')
28             {
29                 num_t++;
30                 loc_t=i;
31             } else if(str[i]!='A')
32             {
33                 other++;
34             }
35         
36         }
37             if((num_p!=1)||(num_t!=1)||(other!=0)||(loc_t-loc_p<=1))
38             {
39                 printf("NO
");
40                 continue;
41             }
42             
43             int x=loc_p,y=loc_t-loc_p-1,z=len-loc_t-1;
44             if(z-x*(y-1)==x)
45             {
46                 printf("YES
");
47             }else
48             {
49                 printf("NO
");
50             }
51     }
52     
53     return 0;
54 }
原文地址:https://www.cnblogs.com/ligen/p/4305964.html