" Right answer " is the most joy automatic reply sentence topic given system. This title belongs to the PAT of " right answer " big delivery - just read the string following conditions are satisfied, the system will output " right answer ", otherwise outputs " Wrong Answer ."
Get the " right answer " conditions are:
- String must have only
P
,A
,T
three characters can not contain other characters; - Arbitrary shape such as
xPATx
a string can get " correct answer ", whereinx
either the empty string, or only by the letterA
string thereof; - If you
aPbTc
are correct, thenaPbATca
it is correct, whicha
,b
,c
are either empty string, or only by the letterA
string composed.
Now ask you to write a PAT referee program automatically determines which strings can get " correct answer " is.
Input formats:
Each test comprises a test input. Line 1 is given a positive integer n- (<10), is the number of strings to be detected. Next, one row for each string, the string length of not more than 100, no spaces.
Output formats:
The detection result row for each string, if the string can get " correct answer ", the output YES
, otherwise the output NO
.
Sample input:
8
PAT
PAAT
AAPATAA
AAPAATAAAA
xPATx
PT
Whatever
APAAATAA
Sample output:
YES
YES
YES
YES
NO
NO
NO
NO
Code
// b1003-我要通过!.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
// PAT APATA AAPATAA AAAPATAAA AAAAPAATAAAAAAAA
// 由题意可知,p前面的A的个数①乘以中间的A的个数②等于后面的A的个数③
#include <iostream>
using namespace std;
bool judge(string str) {
int a1 = 0, a2 = 0, a3 = 0;//统计三个位置a的个数
int p = 0, t = 0;
for (int i = 0; i < str.length(); i++) {
if (p == 0) {
//第一段
if (str[i] == 'A') {
a1++;
}
else if (str[i] == 'P') {
p = 1;
}
else {
return false;
}
}
else {
if (t == 0) {
//中间那段
if (str[i] == 'A') {
a2++;
}
else if (str[i] == 'T') {
t = 1;
}
else {
return false;
}
}
else {
//最后一段
if (str[i] == 'A') {
a3++;
}
else {
return false;
}
}
}
}
if (a2 < 1) {
return false;
}
if (a1 * a2 == a3) {
return true;
}
else {
return false;
}
}
int main()
{
int n;
cin >> n;
for (int i = 0; i < n; i++) {
string str;
cin >> str;
if (judge(str)) {
cout << "YES"<<endl;
}
else {
cout << "NO" << endl;
}
}
return 0;
}