1003 我要通过!(20)(20 分)
“答案正确”是自动判题系统给出的最令人欢喜的回复。本题属于PAT的“答案正确”大派送 —— 只要读入的字符串满足下列条件,系统就输出“答案正确”,否则输出“答案错误”。
得到“答案正确”的条件是:
1. 字符串中必须仅有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
#include<iostream>
#include<string>
#include<set>
#include<ctime>
using namespace std;
void check1() {
string str;
cin >> str;
string::iterator it;
//第一步:如果str含有PAT,在传入
if(str.find('P') != string::npos&&str.find('A') != string::npos&&str.find('T') != string::npos) {
it = str.begin();
while (it!=str.end())
{
if (*it == 'P' || *it == 'A' || *it == 'T') {
it++;
}
else {
break;
}
}
if(it==str.end())
cout << "YES";
}
else {
cout << "NO";
}
}
void check() {
string str;
set<char> ans;
cin >> str;
clock_t startTest = clock();
for (string::iterator it = str.begin();it != str.end();it++)
ans.insert(*it);
set<char>::iterator it = ans.begin();
if (*it == 'A')
it++;
if (*it == 'P')
it++;
if (*it == 'T')
it++;
if (it == ans.end())
cout << "YES";
else
cout << "NO";
clock_t endTest = clock();
cout << double(endTest - startTest) / CLOCKS_PER_SEC << "s" << endl;
}
int main() {
int n;
cin >> n;
while(n--)
check();
system("pause");
return 0;
}
这两个都可以运行出来,但是传入PTA的题库验证时:
check1运行超时;
check()运行错误。
其他:
#include <iostream>
#include <string>
#include<map>
using namespace std;
int main() {
int n, p = 0, t = 0;
string s;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> s;
map<char, int> m;
for (int j = 0; j < s.size(); j++) {
m[s[j]]++;
if (s[j] == 'P')
p = j;
if (s[j] == 'T')
t = j;
}
if (m['P'] == 1 && m['A'] != 0 && m['T'] == 1 && m.size() == 3 && p * (t - p - 1) == s.length() - t - 1)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
这个,运行正确了。。。。。。