1003 我要通过! (20 分)
“答案正确”是自动判题系统给出的最令人欢喜的回复。本题属于 PAT 的“答案正确”大派送 —— 只要读入的字符串满足下列条件,系统就输出“答案正确”,否则输出“答案错误”。
得到“答案正确”的条件是:
- 字符串中必须仅有
P
、A
、T
这三种字符,不可以包含其它字符; - 任意形如
xPATx
的字符串都可以获得“答案正确”,其中x
或者是空字符串,或者是仅由字母A
组成的字符串; - 如果
aPbTc
是正确的,那么aPbATca
也是正确的,其中a
、b
、c
均或者是空字符串,或者是仅由字母A
组成的字符串。
现在就请你为 PAT 写一个自动裁判程序,判定哪些字符串是可以获得“答案正确”的。
通过对样例的分析,很清楚就可以发现规律
p前面的A的个数乘以P与T之间A的个数要等与T之后的A的个数。
#include<iostream>
using namespace std;
int main()
{
int n;
cin >> n;
string cherr[20];
int P = 0;
int A = 0;
int T = 0;
for (int i = 1; i <= n; i++)
{
cin >> cherr[i];
}
for (int i = 1; i <= n; i++)
{
P = 0;
A = 0;
T = 0;
int countP = 0;
int countA = 0;
int countT = 0;
int first = 0;
int last = 0;
for (int count = 0; cherr[i][count] != '\0'; count++)
{
if (cherr[i][count] != 'P' && cherr[i][count] != 'A' && cherr[i][count] != 'T')
{
break;//不是PTA中任意字符的判断
}
if (cherr[i][count] == 'A'&&P==0)
{
first++;//P之前A的个数的判断
}
if (cherr[i][count] == 'P')
{
P = 1;
countP++;//p数目
}
if (cherr[i][count] == 'A' && P == 1&&T==0)
{
A = 1;
countA++;//P与T之间A的数目计算
}
if (cherr[i][count] == 'T' && P == 1 && A == 1)
{
T = 1;
countT++;
}
if (cherr[i][count] == 'A' && P == 1 && A == 1 && T == 1)
{
last++;//T之后A的数目计算
}
}
if (i == n)
{
if (P == 1 && A == 1 && T == 1&&countP==1&&countT==1&& (first * countA == last)&&countA!=0)
{
cout << "YES";
}
else
{
cout << "NO";
}
}
else
{
if (P == 1 && A == 1 && T == 1 && countP == 1 && countT == 1 &&(first*countA==last) && countA != 0)
{
cout << "YES" << endl;
}
else
{
cout << "NO" << endl;
}
}
}
return 0;
}