PAT (Basic Level) Practice (中文)1003 我要通过! (20分) (从编程题目挖掘幼儿算数)

1.题目

答案正确”是自动判题系统给出的最令人欢喜的回复。本题属于 PAT 的“答案正确”大派送 —— 只要读入的字符串满足下列条件,系统就输出“答案正确”,否则输出“答案错误”。

得到“答案正确”的条件是:

  1. 字符串中必须仅有 P、 A、 T这三种字符,不可以包含其它字符;
  2. 任意形如 xPATx 的字符串都可以获得“答案正确”,其中 x 或者是空字符串,或者是仅由字母 A 组成的字符串;
  3. 如果 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

2.题目分析

1.开始按照题目要求完成了输入样例的输出,结果全错…………

经过查阅了解题目中蕴含的规律(参见https://www.cnblogs.com/Anber82/p/11112682.html) 

规律1:字符串中只能有一个P,一个T,若干(大于0)个A

规律2:字符串中不可以有除P、A、T之外别的字符

规律3:P前面A的个数为a,P到T之间的A的个数为b,T后A的个数为c,若a*b==c即符合要求(不服不行……)

3.代码

#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
using namespace std;
int main()
{
	int n;
	cin >> n;

	for (int i = 0; i < n; i++)
	{
		bool d = false;
		string temp;
		cin >> temp;
		int a = 0, b = 0, c = 0;
		if (count(temp.begin(), temp.end(), 'P')>1 || count(temp.begin(), temp.end(), 'T')>1) { cout << "NO" << endl; continue; }
		if(count(temp.begin(), temp.end(), 'P')<1|| count(temp.begin(), temp.end(), 'A')<1|| count(temp.begin(), temp.end(), 'T')<1) { cout << "NO" << endl; continue; }
		for (int j = 0; j < temp.length(); j++)
			if (temp[j] != 'P'&&temp[j] != 'A'&&temp[j] != 'T') { cout << "NO" << endl; d = true; break; }
		if (d == true)continue;
		a = temp.find('P');
		c = temp.length()-1-temp.find('T');
		b = temp.find('T') - temp.find('P') - 1;
		if (a*b == c)cout << "YES" << endl;
		else cout << "NO" << endl;
	}


}

 

posted @ 2020-02-11 12:03  Jason66661010  阅读(101)  评论(0编辑  收藏  举报