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。 输入样例: 8 PAT PAAT AAPATAA AAPAATAAAA xPATx PT Whatever APAAATAA 输出样例: YES YES YES YES NO NO NO NO 题目的三条规则中只有第三条意思比较难懂,其实就是在形如aPbTc的字符串中,P和T中间每多一个字母A,T后就要多一个字符串a,字符串abc都是仅由A组成的,或者是空字符串。 总结起来其实就是 字符串为“PAT”及其衍生的一系列的,而衍生的字符串符合的规则一定是 P之前的A的数量(称为before)乘以PT之间的A的数量(称为inner)等于T之后的A的数量(称为after),即after=before*inner; 例如:PAT before = 0 inner = 1 after = 0 0=1*0 APATA before = 1 inner = 1 after = 1 1=1*1 所以不符合题意的有: 1、只含有PAT三个其中一个或者两个的; 2、含有除PAT外别的字符的 3、P和T位置不对的(T在P前面) 4、after!=inner*before
以上转自:https://blog.csdn.net/zxm490484080/article/details/52832202
#include <stdio.h>
#include <string.h>
#define true 1
#define false 0
int main()
{
int n,before,inner,after;
char c;
char s[100];
int i;
int x;
scanf("%d",&n);
while(n > 0)
{
scanf("%s",s);
i = 0;
before = 0;
inner = -1;
after = -1;
x = true;
while(s[i] != '\0')
{
c = s[i];
if(c != 'P' && c != 'A' && c != 'T') {x = false;break;}
else if(c == 'P' && inner == -1) inner = 0;
else if(c == 'A' && inner == -1) before++;
else if(c == 'A' && after == -1) inner++; //since inner != -1
else if(c == 'A' && after != -1) after++;
else if(c == 'T' && inner != -1 && after == -1 ) after = 0;
else {x = false;break;}
i++;
}
//printf("%d %d %d %d",before,inter,after,x);
if( x && inner > 0 && after >= 0 && before * inner == after) printf("YES\n");
else printf("NO\n");
n--;
}
return 0;
}