[PAT-乙级]1003.我要通过!

来源:互联网 发布:淘宝物流业务流程图 编辑:程序博客网 时间:2024/05/20 11:36

1003. 我要通过!(20)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
CHEN, Yue

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

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

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

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

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

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

输入样例:
8PATPAATAAPATAAAAPAATAAAAxPATxPTWhateverAPAAATAA
输出样例:
YESYESYESYESNONONONO
首先满足条件:所有的字符只能由P、A、T三个构成。
用count_p表示:P之前A的个数,
用count_a表示:P和T之间A的个数,
用count_t表示:T之后A的个数,
需要满足count_p*count_a == count_t并且count_a >= 1
#include<stdio.h>#include<string.h>int main(){    freopen("D://input.txt", "r", stdin);    int n;    while(scanf("%d", &n) != EOF)    {        while(n --)        {            char s[102];            scanf("%s", s);            int pos_p = -1, pos_t = -1;            int count_p = 0, count_a = 0, count_t = 0;            bool flag_p = true, flag_t = true;            for(int i = 0; i < strlen(s); i ++)            {                if(s[i] == 'P' && flag_p)                {                    pos_p = i;                    flag_p = false;                }                if(s[i] == 'T' && flag_t)                {                    pos_t = i;                    flag_t = false;                }            }            if((pos_p == -1 || pos_t == -1) || pos_p > pos_t)            {                printf("NO\n");                continue;            }            else            {                for(int i = 0; i < pos_p; i ++)                {                    if(s[i] == 'A')                        count_p ++;                }                for(int i = pos_p+1; i < pos_t; i ++)                {                    if(s[i] == 'A')                        count_a ++;                }                for(int i = pos_t+1; i < strlen(s); i ++)                {                    if(s[i] == 'A')                        count_t ++;                }                if((count_p + count_a + count_t + 2) != strlen(s) || count_a == 0)                {                    printf("NO\n");                    continue;                }            }            if((count_p * count_a) == count_t)                printf("YES\n");            else                printf("NO\n");        }    }    return 0;}


0 0
原创粉丝点击