This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#include <ext/rope>
 
using namespace __gnu_cxx; 
using namespace std;
 
#define debug(x) cerr << " - " << #x << ": " << x << '\n';
#define debugs(x, y) cerr << " - " << #x << ": " << x << " " << #y << ": " << y << '\n';
#define debugv(v) cerr << #v << " : " << "[ "; for(auto it : v) cerr << it << " ,"; cerr << ']' << "\n";
 
int main(){
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int tt;
  cin >> tt;
  while(tt--){
    string mm ; 
    cin >> mm; 
    int n = mm.length();
    map<char, vector<int>>mp;
    map<char , int>start;
    for(int i = 0 ; i < n; i++){
      mp[mm[i]].push_back(i);        
      start[i] = 0;
    }
    crope ss;
    for(int i = 0 ; i < n ; i++){
      ss+=(mm[i]);  
    }
    int ans = 1;
    for(int i = n-1,qs = 0 ; qs< n/2 ; i--,qs++){
      char q = mm[i];
      crope acc;
      bool is = 1;
      int idx = qs;
      if(mp[q][start[q]]<qs)start[q] = lower_bound(mp[q].begin(),mp[q].end(),qs)-mp[q].begin();
      for(int j = start[q] ; j < (int)mp[q].size() ; j++){
        if(mp[q][j]>=n/2)break;
        acc += ss.substr(idx , mp[q][j]-idx+1);
        int rs = acc.length();
        idx = mp[q][j]+1;
        if((acc[0] != ss[i-rs+1]) || (acc[rs-1]!=ss[i])){
          start[q] = j+1;
          continue;  
        }
        if(ss.substr(i-rs+1 , rs) == acc){
          acc.clear();
          ans+=2;
          i-=rs;
          i++;
          qs = mp[q][j];
          if(qs+1==i)ans--;
          is = 0;
          break;  
        }
        start[q] = j+1;
      }
      if(is)break;        
    }
    cout << ans << '\n';
  }
  return 0;
}
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |