Advertisement
Dmaxiya

回文字符串 参考代码

Mar 12th, 2025
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.31 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef long long LL;
  5. const int maxn = 1000000 + 100;
  6. int T, n, l, r;
  7. bool flag;
  8. char str[maxn];
  9.  
  10. bool equalLqb(char ch) {
  11.     return ch == 'l' || ch == 'q' || ch == 'b';
  12. }
  13.  
  14. int main() {
  15. #ifdef ExRoc
  16.     freopen("test.txt", "r", stdin);
  17. #endif
  18.     ios::sync_with_stdio(false);
  19.  
  20.     scanf("%d", &T);
  21.     while (T--) {
  22.         scanf("%s", str + 1);
  23.         n = strlen(str + 1);
  24.         l = 0;
  25.         for (int i = 1; i <= n; ++i) {
  26.             if (!equalLqb(str[i])) {
  27.                 l = i;
  28.                 break;
  29.             }
  30.         }
  31.         r = n + 1;
  32.         for (int i = n; i >= 1; --i) {
  33.             if (!equalLqb(str[i])) {
  34.                 r = i;
  35.                 break;
  36.             }
  37.         }
  38.         if (l == 0) {
  39.             printf("Yes\n");
  40.             continue;
  41.         }
  42.         if ((r - l + 1) % 2 == 1) {
  43.             l = r = (l + r) / 2;
  44.         } else {
  45.             l = (l + r) / 2;
  46.             r = l + 1;
  47.         }
  48.         flag = true;
  49.         for (; l >= 1 && r <= n; --l, ++r) {
  50.             if (str[l] != str[r]) {
  51.                 flag = false;
  52.                 break;
  53.             }
  54.         }
  55.         if (!flag || l != 0) {
  56.             printf("No\n");
  57.         } else {
  58.             printf("Yes\n");
  59.         }
  60.     }
  61.  
  62.     return 0;
  63. }
  64.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement