我要通过!(20)

题意:

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

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

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

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

分析:

1、对于第一个条件,可知,字符串中有其他字符则输出NO。

2、对于第二个条件,可知

(1)首先,因为x 或者是空字符串,或者是仅由字母 A 组成的字符串,所以P和T的个数只能出现一次,超过一次或没出现都是NO,且P的位置需要在T前面。

(2)假设P之前的A的个数为a,P与T之间的A的个数为b,T之后的A的个数为c。形如 xPATx 的字符串,这里PAT前后的x指的是同一个字符串,即a与c是相等的,这里可以设他们都为x个。而b初始是为1的。

3、对于第三个条件, 可知,

(1)如果 aPbTc 是正确的,那么 aPbATca 也是正确的,可知,b每增加1个,c就增加x个。

即当b为1时,c为x,

当b为2时,c为2x,

当b为3时,c为3x,……而a一直是x个,因此,当c%a==0且c/a==b时为YES。

(2)注意,当a与c都为0的时候,即x为0的时候,b每增加1个,c就增加0个,即当a与c都为0的时候,P与T之间可以有许多个A,唯独不能没有A,此处需要特判。

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define lowbit(x) (x & (-x))
const double eps = 1e-8;
inline int dcmp(double a, double b){
    if(fabs(a - b) < eps) return 0;
    return a > b ? 1 : -1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const int MAXN = 100 + 10;
const int MAXT = 10000 + 10;
using namespace std;
char s[MAXN];
bool judge(char x){
    return x == 'P' || x == 'A' || x == 'T';
}
int main(){
    int n;
    scanf("%d", &n);
    while(n--){
        scanf("%s", s);
        int len = strlen(s);
        bool ok = true;
        int cnt1 = 0, cnt2 = 0;
        for(int i = 0; i < len; ++i){
            if(!judge(s[i])){
                ok = false;
                break;
            }
            if(s[i] == 'P') ++cnt1;
            if(s[i] == 'T') ++cnt2;
            if(cnt1 == 2 || cnt2 == 2){
                ok = false;
                break;
            }
        }
        if(!ok) printf("NO\n");
        else{
            int id1 = -1, id2 = -1;
            for(int i = 0; i < len; ++i){
                if(s[i] == 'P'){
                    id1 = i;
                }
                if(s[i] == 'T'){
                    id2 = i;
                }
            }
            if(id1 == -1 || id2 == -1 || (id1 != -1 && id2 != -1 && id1 > id2)){
                printf("NO\n");
                continue;
            }
            int a = id1;
            int b = id2 - id1 - 1;
            int c = len - a - b - 2;
            if(a == 0 && c == 0){
                if(b == 0) printf("NO\n");
                else printf("YES\n");
                continue;
            }
            if(c % a == 0 && c / a == b){
                printf("YES\n");
            }
            else{
                printf("NO\n");
            }
        }
    }
    return 0;
}

 

posted @ 2017-12-04 12:30  Somnuspoppy  阅读(173)  评论(0编辑  收藏  举报