题目:https://pintia.cn/problem-sets/994805260223102976/problems/994805323154440192
一开始以为是个字符串题,就按照传统字符串的方法去做了,事实证明看不出问题的本质换个方法会复杂N多!!!

思路

使用数字来写就简单很多了,只需要发现规律z - x * (y - 1) = x就可以了
原因是y每增加一个,z就增加x个
只要最后中间只有一个A的时候,左右两边相等即可

代码

  1. #include<string>
  2. #include<algorithm>
  3. #include<iostream>
  4. #include<cstdio>
  5. #include<cstring>
  6. using namespace std;
  7. int main(){
  8. int n;
  9. string str;
  10. scanf("%d",&n);
  11. for(int i = 0; i < n; i++){
  12. cin>>str;
  13. int len = str.size();
  14. int num_p = 0, num_t = 0, other = 0;
  15. int pos_p = -1, pos_t = -1;
  16. for(int i = 0; i < len; i++){
  17. if(str[i]=='P'){
  18. num_p++;
  19. pos_p = i;
  20. }
  21. else if(str[i]=='T'){
  22. num_t++;
  23. pos_t = i;
  24. }
  25. else if(str[i]!='A') other++;
  26. }
  27. if(num_p!=1||num_t!=1||other||pos_t - pos_p <= 1){
  28. printf("NO\n");
  29. continue;
  30. }
  31. int x = pos_p, y = pos_t - pos_p - 1, z = str.size() - pos_t - 1;
  32. if(z - x * (y - 1) == x) printf("YES\n");
  33. else printf("NO\n");
  34. }
  35. return 0;
  36. }