此题核心是找到三个位置(xPyTz)x,y,z中A的数量关系
半个月之前做过一次,当时做不出来就放下了,后两个条件真的难懂......
自己写代码的时候,xyz三个数值在每次迭代的时候没有更新,记得要更新!!!!!
另外,不仅像书里说的,z-(y-1)*x==x,还要 y!=0 作为判定条件
#include<iostream>
using namespace std;
bool con1(const string &s)
{
auto begin = s.begin();
auto end = s.end();
char c;
for (; begin != end;begin++)
{
c = *begin;
if(c!='P'&&c!='A'&&c!='T')
return 0;
}
return 1;
}
int main()
{
int n;
bool flag=0;
string s[1024];
string::iterator begin;
string::iterator end;
int x = 0, y = 0, z = 0;
cin >> n;
for (int i = 0; i < n;i++)
cin >> s[i];
for (int i = 0; i < n;i++)
{
flag=0;
if(con1(s[i]))
{
begin = s[i].begin();
end = s[i].end();
for (; *begin!='P' && begin != end;begin++)
x++;
begin++;
for (; *begin!='T' && begin != end;begin++)
y++;
begin++;
for (; begin != end;begin++)
z++;
if(z-(y-1)*x==x&&y)
flag=1;
x = 0, y = 0, z = 0;//记得重置变量
}
if(flag)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
getchar();
}
来源:CSDN
作者:鸡蛋鸡蛋鸡蛋青
链接:https://blog.csdn.net/qq_41228018/article/details/104343417