我要通过

来源:互联网 发布:linq 更新数据 编辑:程序博客网 时间:2024/06/08 00:40
“答案正确”是自动判题系统给出的最令人欢喜的回复。本题属于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

#include <iostream>#include <stdio.h>#include <string.h>using namespace std;int main(){   // freopen("C:\\Users\\Public\\code\\datain.txt","r",stdin);    int count_a,count_p,count_t,pos_p,pos_t;    int n;    char str[101];    while(~scanf("%d",&n)){        while(n--){            scanf("%s",str);            count_a=count_p=count_t=pos_p=pos_t=0;            int m=strlen(str);            for(int i=0;i<m;i++){                if(str[i]=='P'){                    count_p++;                    pos_p=i;                }                else if(str[i]=='A'){                    count_a++;                }                else if(str[i]=='T'){                    count_t++;                    pos_t=i;                }            }            //cout<<"a"<<count_a<<endl<<"p"<<count_p<<endl<<"t"<<count_t<<endl<<"pos_p"<<pos_p<<endl<<"pos_t"<<pos_t<<endl;            if(count_a+count_p+count_t!=m||count_a!=m-2||pos_t<=pos_p+1||pos_p*(pos_t-pos_p-1)!=m-1-pos_t)                printf("NO\n");            else                printf("YES\n");        }    }    return 0;}



原创粉丝点击