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>
using namespace std;
#define int int64_t
#define pb push_back
#define pii pair<int, int>
#define st first
#define nd second
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
const int CNST = 31;
const int MOD = 1e9+7;
int pot[1000009];
int solve(string a) {
int h1 = 0;
int h2 = 0;
int s = 0;
int e = sz(a)-1;
int cur = 0;
int ans = 0;
int S = 0;
int E = 0;
while(s<e) {
h1 += (a[s]-'a'+1)*pot[cur];
h1 %= MOD;
h2 *= CNST;
h2 += (a[e]-'a'+1);
h2 %= MOD;
if(h1==h2) {
h1 = h2 = cur = 0;
ans+=2;
S = s;
E = e;
} else {
cur++;
}
s++;
e--;
}
return ans+(S+1!=E);
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
pot[0] = 1;
for(int i=1;i<1000009;i++) pot[i]=pot[i-1]*CNST%MOD;
int t;
cin >> t;
while(t--) {
string s;
cin >> s;
cout << solve(s) << "\n";
}
}
# | 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... |