“答案正确”是自动判题系统给出的最令人欢喜的回复。本题属于PAT的“答案正确”大派送 —— 只要读入的字符串满足下列条件,系统就输出“答案正确”,否则输出“答案错误”。
得到“答案正确”的条件是:
1. 字符串中必须仅有P, A, T这三种字符,不可以包含其它字符;
2. 任意形如 xPATx 的字符串都可以获得“答案正确”,其中 x 或者是空字符串,或者是仅由字母 A 组成的字符串;
3. 如果 aPbTc 是正确的,那么 aPbATca 也是正确的,其中 a, b, c 均或者是空字符串,或者是仅由字母 A 组成的字符串。
输入格式: 每个测试输入包含1个测试用例。第1行给出一个自然数n (<10),是需要检测的字符串个数。接下来每个字符串占一行,字符串长度不超过100,且不包含空格。
输出格式:每个字符串的检测结果占一行,如果该字符串可以获得“答案正确”,则输出YES,否则输出NO。
输入样例:8 PAT PAAT AAPATAA AAPAATAAAA xPATx PT Whatever APAAATAA输出样例:
YES YES YES YES NO NO NO NO
CPP
#include <cstring> #include <algorithm> #include <cstdio> using namespace std; bool Judge(const char * str){ int Len = strlen(str); int posP,posT; for(posP=0;str[posP]=='A' && posP<Len;++posP);//找第一个P for(posT=posP+1;str[posT]=='A' && posT<Len;++posT);//找第一个T if(str[posP]!='P' || str[posT]!='T') return false; int a = posP,b = posT-posP-1,c=Len-posT-1; if(a==c && b!=0) return true; if(a!=0 && c/a==b) return true; return false; } int main() { freopen("E:\\test.txt","r",stdin); int N; char str[1000]={0}; scanf("%d",&N); while(N--){ scanf("%s",str); if(Judge(str)) printf("YES\n"); else printf("NO\n"); } return 0; }
估计是最傻的写法了
import re def check(spat): if spat == 'PAT': return True match = re.match(r'(A*)PA(A*)T(A*)', spat) if match: l, c, r = match.group(1), match.group(2), match.group(3) if l == r and c == "": return True return check(l+"P"+c+"T"+"A"*(len(r)-len(l))) for i in range(int(raw_input())): if check(raw_input()): print 'YES' else: print 'NO'