Ankit_132

E

Jun 26th, 2024
906
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.00 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4.  
  5. vector<int> z_function(string s) {
  6.     int n = s.size();
  7.     vector<int> z(n);
  8.     int l = 0, r = 0;
  9.     for(int i = 1; i < n; i++) {
  10.         if(i < r) {
  11.             z[i] = min(r - i, z[i - l]);
  12.         }
  13.         while(i + z[i] < n && s[z[i]] == s[i + z[i]]) {
  14.             z[i]++;
  15.         }
  16.         if(i + z[i] > r) {
  17.             l = i;
  18.             r = i + z[i];
  19.         }
  20.     }
  21.     return z;
  22. }
  23.  
  24.  
  25. void solve() {
  26.     string s, t; cin >> s;
  27.     int n = s.size();
  28.     if(n & 1) {
  29.         cout << "0\n"; return;
  30.     }
  31.    
  32.     t = s;
  33.     reverse(t.begin(), t.end()) ;
  34.    
  35.     auto z1 = z_function(s);
  36.     auto z2 = z_function(t);
  37.  
  38.     int ans = 0 ;
  39.     for (int sz1 = 0; sz1 <= n / 2; sz1++ ) {
  40.         int sz2 = (n / 2) - sz1 ;
  41.        
  42.         if(z1[sz1] >= sz1 && z2[sz2] >= sz2)
  43.             ans++ ;
  44.     }
  45.     cout << ans << endl;
  46.    
  47.    
  48. }
  49.  
  50. int main() {
  51.     // your code goes here
  52.     int t; cin >> t;
  53.     while(t--) solve();
  54.  
  55. }
Advertisement
Add Comment
Please, Sign In to add comment