您的位置:首页 > 其它

1003. 我要通过!(20)

2015-09-19 15:02 113 查看
答案正确”是自动判题系统给出的最令人欢喜的回复。本题属于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

#include "stdafx.h"
#include "iostream"	
#include "cstring"
#include "string"
const int MAX = 100;
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
	int n,i,j;
	int temp;
	int countA, countB, countT, countP;
	cin >> n;
	char shuru[10][MAX];
	string shuchu[10];
			
		for (i = 0; i < n; i++)
		{
			countA = countP= countT=countB=0;
			cin >> shuru[i];
			temp =strlen(shuru[i]);			
				for (j = 0; j < temp; j ++ )
				{
					if (shuru[i][j] == 'A')
						countA++;
					else if (shuru[i][j] == 'P')
						countP++;
					else if (shuru[i][j] == 'T')
						countT++;
					else
						countB++;
				}			
			if (countA>0 && countB == 0 && countP>0 && countT>0)
			{
				shuchu[i] = "YES";
				//strcpy(shuchu[i], "YES");
			}
			else
			{
				shuchu[i] = "NO";
				//strcpy(shuchu[i], "NO");
			}
		}
	
	for ( i = 0; i < n; i++)
	{
		cout << shuchu[i] << endl;
	}
		return 0;
}
<span style="font-size:14px;"><strong><span style="font-family:Droid Sans, Verdana, Microsoft YaHei, Tahoma, sans-serif;">Adeng:这是我第一次编写的,很烂,而且还是错的。。。这题我思考了很久也不明白题意,后来经过网上查询才了解到:</span><span style="line-height: 24px; background-color: rgb(237, 237, 237);"><span style="font-family:FangSong_GB2312;">我们把可通过字符串以aPbTc来表示,a,c分别表示A*(A的闭包),其中b表示A+ (A的非空闭包),则a,b,c中A的个数满足关系 len(a) * len(b) = len(c);</span></span></strong></span>
<span style="font-size:14px;"><strong><span style="line-height: 24px; background-color: rgb(237, 237, 237);"><span style="font-family:FangSong_GB2312;"></span></span></strong></span><pre name="code" class="cpp">#include <string>
<pre name="code" class="cpp">#include "stdafx.h"
#include <string>
#include <iostream>
using namespace std;

int main()
{
	int n;
	cin >> n;
	while (n--)
	{
		string s;
		cin >> s;
		size_t p = s.find_first_not_of("A");
		if ((p == string::npos) || (s[p] != 'P'))  //找到第一个非字符“A”的字符的位置,如果不是“P”直接判错
		{
			cout << "NO" << endl;
			continue;
		}
		size_t t = s.find_first_not_of("A", p + 1);//从“P”后面的字符开始查询第一不是“A”的字符的位置
		if ((t == string::npos) || (t == p + 1) || (s[t] != 'T'))//如果没找到或者"P"后面的字符不是“A”又或者该字符不是“T”判错
		{
			cout << "NO" << endl;
			continue;
		}
		size_t n = s.find_first_not_of("A", t + 1);//从“T”往后查询非“A”字符
		if (n != string::npos) //如果查询到了非“A”字符则判错
		{
			cout << "NO" << endl;
			continue;
		}
		if ((s.length() - t - 1) == p * (t - p - 1))//我们把可通过字符串以aPbTc来表示,a,c分别表示A*(A的闭包),其中b表示A+ (A的非空闭包),则a,b,c中A的个数满足关系 len(a) * len(b) = len(c)
		{
			cout << "YES" << endl;
		}
		else
		{
			cout << "NO" << endl;
		}
	} 
	return 0;

}





                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: