pat 乙 1003. 我要通过!

本文介绍了一种用于判断字符串是否符合特定模式的算法。该算法通过输入一系列包含字符P、A、T的字符串,并检查这些字符串是否满足特定条件:即一个P字符、若干A字符和一个T字符的组合,且P到T的距离乘以A的数量等于字符串尾部的长度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

关键理解前面乘以中间的个数等于后面的个数
#include<iostream>
#include<string>
using namespace std;


int main()
{
	int n;
	cin>>n;
	
	string tmp;
	
	while(n--)
	{
		cin>>tmp;
		
		int index_p,index_t;
		int count_p,count_t,count_a;
		count_p=count_t=count_a=0;
		
		
		for(int i=0;i<tmp.size();i++)
		{
			if(tmp.at(i)=='P')
			{
				index_p=i;
				count_p++;
			}
			else if(tmp[i]=='A')
			{
				count_a++;
			}
			else if(tmp[i]=='T')
			{
				count_t++;
				index_t=i;
			}
		}
		
		if(count_p+count_a+count_t!=tmp.size() || count_p!=1 || count_t!=1 || count_a==0||
		index_p*(index_t-index_p-1)!=tmp.size()-index_t-1
		)
		
		cout<<"NO"<<endl;
		
		else cout<<"YES"<<endl;
	}
	
	
	return 0;
 } 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值