PAT1003

本文介绍了一个C++程序,用于检查输入字符串是否符合特定模式:仅包含一个'T'和一个'P'字符,并确保'T'与'P'之间的子串长度乘以其前缀长度等于后缀长度。文章通过示例展示了如何运行此程序并判断字符串是否满足条件。
#include <bits/stdc++.h>

using namespace std;

bool check1(string s){
    int countT = 0,countP = 0;
    for(int i = 0;i<s.size();i++){
        if(s[i] == 'P') countT++;
        else if(s[i] == 'T') countP++;
        else if(s[i]!='A') return false;
    }
    if(countT!=1 && countP!=1) return false;
    return true;
}

int main() {
    int n;
    string s;
    cin>>n;
    while (n--)
    {
        cin>>s;
        if(check1(s))
        {
            int pos1 = s.find('P',0);
            int pos2 = s.find('T',0);
            string a = s.substr(0,pos1);
            string b=  s.substr(pos1+1,pos2-pos1-1);
            string c = s.substr(pos2+1,s.size()-pos2-1);
            if(b.size()>=1 && a.size()*b.size() == c.size())
            {
                cout<<"YES"<<endl;
                continue;
            }
        }
        cout<<"NO"<<endl;
    }
    int pos1,pos2;

    return 0;
}
/**
8
PAT
PAAT
AAPATAA
AAPAATAAAA
xPATx
PT
Whatever
APAAATAA
 **/

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值