/*
* @Author:365JHWZGo
* @Description: 1003 我要通过! (20 分)
* @Date: 2021-08-30 15:15:16
* @FilePath: \VScodeC++practice\PAT\iwantwin.cpp
* @LastEditTime: 2021-08-30 17:52:50
* @LastEditors: 365JHWZGo
*/
#include <iostream>
using namespace std;
bool judge(string s)
{
//p为P最后一次出现的位置
//t为T最后一次出现的位置
//a为P和T之间A的个数
int p, a = 0, t;
//ptimes为P出现次数
//ttimes为T出现次数
int ptimes = 0, ttimes = 0;
//当字符串长度为3时,只能为PAT
if (s.size() == 3 && s != "PAT")
return false;
//当字符串长度<3时,错误
if (s.size() < 3)
return false;
//当字符串包含除P,A,T以外字符时则错误,否则记录P,T次数以及位置
for (int i = 0; i < s.size(); i++)
{
if (s[i] != 'P' && s[i] != 'A' && s[i] != 'T')
return false;
if (s[i] == 'P')
{
p = i;
ptimes++;
}
if (s[i] == 'T')
{
t = i;
ttimes++;
}
}
//当P,T次数不为1时则错误
//此时不需要判断P...T之间是否都为A,因为字符串由P,A,T组成,且P,T只出现1次,剩下的则全为A
if (ttimes != 1 || ptimes != 1)
return false;
//当T在P之前出现则错误
if (p > t)
return false;
//f记录P...T之前的字符串
//pt记录P..T字符串
//a记录P..T之间A的个数
string f = "", pt = "";
for (int i = 0; i < s.size(); i++)
{
if (i < p)
f += s[i];
if (i >= p && i <= t)
pt += s[i];
if (i > p && i < t)
a++;
}
//字符串不是 xPATx类型 && aPbATca类型(不需要考虑aPbTc,因为满足aPbATca类型一定满足aPbTc)
if (s != f + "PAT" + f && (s.size() - 1 - t) != a * p)
return false;
return true;
}
int main()
{
int times;
cin >> times;
string s;
for (int i = 0; i < times; i++)
{
cin >> s;
if (judge(s))
cout << "YES";
else
cout << "NO";
//最后一行不输出换行
if (i != times - 1)
cout << endl;
}
//system("pause");
return 0;
}
PTA1003 我要通过!满分!详解分类!
最新推荐文章于 2023-08-28 17:00:36 发布