PAT 乙级 1003 我要通过!(20)(20 分)

1003 我要通过!(20)(20 分)

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

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

1. 字符串中必须仅有P, A, T这三种字符,不可以包含其它字符;\

  1. 任意形如 xPATx 的字符串都可以获得“答案正确”,其中 x 或者是空字符串,或者是仅由字母 A 组成的字符串;\
  2. 如果 aPbTc 是正确的,那么 aPbATca 也是正确的,其中 a, b, c 均或者是空字符串,或者是仅由字母 A 组成的字符串。

现在就请你为PAT写一个自动裁判程序,判定哪些字符串是可以获得“答案正确”的。

输入格式: 每个测试输入包含1个测试用例。第1行给出一个自然数n (&lt10),是需要检测的字符串个数。接下来每个字符串占一行,字符串长度不超过100,且不包含空格。

输出格式:每个字符串的检测结果占一行,如果该字符串可以获得“答案正确”,则输出YES,否则输出NO。

输入样例:

8
PAT
PAAT
AAPATAA
AAPAATAAAA
xPATx
PT
Whatever
APAAATAA

输出样例:

YES
YES
YES
YES
NO
NO
NO
NO
# include <cstdio>
# include <cstring>
# include <iostream>
using namespace std;

int main(){
//	freopen("C:\\1.txt", "r", stdin);
	char ch[101];
	int n;
	cin >> n;   // 输入个数 
	while(n--){
		int countp = 0, counta = 0, countt = 0, ca = 0, cp = 0, ct = 0;
		cin >> ch;
		for(int i = 0; ch[i] != '\0'; i++){   // 进行 P A T 三个字母的个数计算 
			if(ch[i] == 'P') countp++, cp++;
			else if(ch[i] == 'A') counta++, ca++;
			else if(ch[i] == 'T') countt++, ct++;
		}
	//	cout << counta << countp << countt << endl << strlen(ch);
	//	无其他字母,P T 只有一个, 有 A 
		if(strlen(ch) == countp + counta + countt && countt == countp && countp == 1 && ca && cp && ct){
			int i = 0;
			if(ch[i]== 'P')//如果第一位是P 肯定是PxT的形式
            {
                int j;
                for(j = 1; j < strlen(ch)-1; j++)//如果中间有不是A的字符 就不符合条件
                    if(ch[j]!='A')
                        break;
                if(j == strlen(ch)-1)
                    cout<<"YES"<<endl;
                else
                    cout<<"NO"<<endl;
                continue;
            }
			
			else if(ch[i] == 'A'){
				int total_1 = 0, total_2 = 0, total_3 = 0, j, k;
				for(j = 0; j < strlen(ch); j++){
					if(ch[j] != 'A')
						break;
						total_1++;
				}
		//			cout << total_1 << endl;
				if(ch[j] != 'P'){
					printf("NO\n");
					continue;
				}
				else{   // 如果 A完了是 P 
					for(k = j+1; k < strlen(ch); k++){
						if(ch[k] != 'A')
							break;
						total_2++;
					}
		//			cout << total_2 << endl; 
					for(k++; k < strlen(ch); k++)
						total_3++;
		//			cout << total_3 << endl;
					if(total_1 * total_2 == total_3)
						printf("YES\n");
					else
						printf("NO\n");
					continue;
				}
			}
			else{
				printf("NO\n");
				continue;
			}
		}
		else{
			printf("NO\n");
			continue;
		}
	}
	return 0;
} 

猜你喜欢

转载自blog.csdn.net/Hello_Mr_X/article/details/81610126