pta1003

1003 我要通过! (20 分)

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

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

  1. 字符串中必须仅有 P、 A、 T这三种字符,不可以包含其它字符;
  2. 任意形如 xPATx 的字符串都可以获得“答案正确”,其中 x 或者是空字符串,或者是仅由字母 A 组成的字符串;
  3. 如果 aPbTc 是正确的,那么 aPbATca 也是正确的,其中 a、 b、 c 均或者是空字符串,或者是仅由字母 A 组成的字符串。

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

通过对样例的分析,很清楚就可以发现规律

p前面的A的个数乘以P与T之间A的个数要等与T之后的A的个数。

#include<iostream>
using namespace std;
int main()
{
	int n;
	cin >> n;
	string cherr[20];
	int P = 0;
	int A = 0;
	int T = 0;
	for (int i = 1; i <= n; i++)
	{
		cin >> cherr[i];
	}
	for (int i = 1; i <= n; i++)
	{	
		P = 0;
		A = 0;
		T = 0;
		int countP = 0;
		int countA = 0;
		int countT = 0;
		int first = 0;
		int last = 0;
		for (int count = 0; cherr[i][count] != '\0'; count++)
		{
			if (cherr[i][count] != 'P' && cherr[i][count] != 'A' && cherr[i][count] != 'T')
			{
				break;//不是PTA中任意字符的判断
			}
			if (cherr[i][count] == 'A'&&P==0)
			{
				first++;//P之前A的个数的判断
			}
			if (cherr[i][count] == 'P')
			{
				P = 1;
				countP++;//p数目
			}
			if (cherr[i][count] == 'A' && P == 1&&T==0)
			{
				A = 1;
				countA++;//P与T之间A的数目计算
			}
			if (cherr[i][count] == 'T' && P == 1 && A == 1)
			{
				T = 1;
				countT++;
			}
			if (cherr[i][count] == 'A' && P == 1 && A == 1 && T == 1)
			{
				last++;//T之后A的数目计算
			}
		}
		if (i == n)
		{
			if (P == 1 && A == 1 && T == 1&&countP==1&&countT==1&& (first * countA == last)&&countA!=0)
			{
				cout << "YES";
			}
			else
			{
				cout << "NO";
			}
		}
		else
		{
			if (P == 1 && A == 1 && T == 1 && countP == 1 && countT == 1 &&(first*countA==last) && countA != 0)
			{
				cout << "YES" << endl;
			}
			else
			{
				cout << "NO" << endl;
			}
		}
	}
	return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值