题目:https://pintia.cn/problem-sets/994805260223102976/problems/994805323154440192
一开始以为是个字符串题,就按照传统字符串的方法去做了,事实证明看不出问题的本质换个方法会复杂N多!!!
思路
使用数字来写就简单很多了,只需要发现规律z - x * (y - 1) = x就可以了
原因是y每增加一个,z就增加x个
只要最后中间只有一个A的时候,左右两边相等即可
代码
#include<string>#include<algorithm>#include<iostream>#include<cstdio>#include<cstring>using namespace std;int main(){int n;string str;scanf("%d",&n);for(int i = 0; i < n; i++){cin>>str;int len = str.size();int num_p = 0, num_t = 0, other = 0;int pos_p = -1, pos_t = -1;for(int i = 0; i < len; i++){if(str[i]=='P'){num_p++;pos_p = i;}else if(str[i]=='T'){num_t++;pos_t = i;}else if(str[i]!='A') other++;}if(num_p!=1||num_t!=1||other||pos_t - pos_p <= 1){printf("NO\n");continue;}int x = pos_p, y = pos_t - pos_p - 1, z = str.size() - pos_t - 1;if(z - x * (y - 1) == x) printf("YES\n");else printf("NO\n");}return 0;}
