”答案正确”是自动判题系统给出的最令人欢喜的回复。本题属于 PAT 的“答案正确”大派送 —— 只要读入的字符串满足下列条件,系统就输出“答案正确”,否则输出“答案错误”。
得到“答案正确”的条件是:
字符串中必须仅有 P、 A、 T这三种字符,不可以包含其它字符;
任意形如 xPATx 的字符串都可以获得“答案正确”,其中 x 或者是空字符串,或者是仅由字母 A 组成的字符串;
如果 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
思路,这题是一题找规律的题目,记前面的A的数目为front,中间A的数目是mid,后面A的数目是rear,只要满足front*mid=rear即可通过。
代码:
#include <iostream>
#include <string>
using namespace std;
bool pass(string str)
{
if (str == "PAT")
{
return true;
}
int index, front, mid, rear;
index = front = mid = rear = 0;
while (str[index]=='A')
{
index++;
front++;//记录前面A的个数
}
if (str[index] == 'P')
{
index++;
}
else
{
return false;
}
while (str[index]=='A')
{
mid++;//记录中间A的个数
index++;
}
if (str[index]=='T')
{
index++;
}
else
{
return false;
}
while (str[index]=='A')
{
rear++;//记录后面A的个数
index++;
}
if (!str[index] && rear == mid * front && mid > 0)
{
return true;
}
return false;
}
int main()
{
int n;
string str;
cin >> n;
for (int i = 0; i < n; i++)
{
cin >>str;
bool flag = pass(str);
if (flag)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
return 0;
}