“答案正确”是自动判题系统给出的最令人欢喜的回复。本题属于 PAT 的“答案正确”大派送 —— 只要读入的字符串满足下列条件,系统就输出“答案正确”,否则输出“答案错误”。
得到“答案正确”的条件是:
- 字符串中必须仅有
P
、A
、T
这三种字符,不可以包含其它字符; - 任意形如
xPATx
的字符串都可以获得“答案正确”,其中x
或者是空字符串,或者是仅由字母A
组成的字符串; - 如果
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
作者: CHEN, Yue
单位: 浙江大学
时间限制: 400 ms
内存限制: 64 MB
代码长度限制: 16 KB
我的代码,扣了两分,有知道的朋友解答一下
#include<iostream>
#include<string>
using namespace std;
bool judge(string focus)
{
int n;
int counter_A=0;
int counter_P=0;
int counter_T=0;
int counter_fir=0;
int counter_sec=0;
int counter_thir=0;
char it;
while(!focus.empty())
{
it=focus[0];
focus.erase(0,1);
if(it=='A')
{
if(counter_P==0&&counter_T==0)
{
counter_fir++;
}
else if(counter_P==1&&counter_T==0)
{
counter_sec++;
}
else if(counter_P==1&&counter_T==1)
{
counter_thir++;
}
else
{
return false;
}
}
else if(it=='P'&&counter_T==0)
{
counter_P++;
}
else if(it=='T'&&counter_P==1)
{
counter_T++;
}
else
{
return false;
}
}
if(counter_sec==0)
{
return false;
}
else if(counter_sec==1)
{
if(counter_fir!=counter_thir)
{
return false;
}
}
else
{
if(counter_fir==0&&counter_thir==0)
{
return true;
}
//else if(counter_fir!=(counter_thir-counter_fir*(counter_sec-1)))
else if(counter_fir*counter_sec!=counter_thir)
{
return false;
}
}
return true;
}
int main()
{
int n;
string rec[10000];
cin>>n;
for(int i=0; i<n; i++)
{
cin>>rec[i];
}
for(int i=0; i<n; i++)
{
if(judge(rec[i]))
{
cout<<"YES"<<endl;
}
else
{
cout<<"NO"<<endl;
}
}
}
网上找了一个大佬的代码,22行完成了我100行代码的任务,还是满分,他用的C++11中的map结构,解题的思路也相当清晰。