#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD = 1000000007;
const int p = 41;
int hsh[1000005];
int powers[1000005];
int add(int a, int b){
return (a+b)%MOD;
}
int mul(int a, int b){
return ((ll)a*b)%MOD;
}
int substring_hash(int l, int r){
if(l == 0) return hsh[r];
return add(hsh[r], mul(powers[r-l+1], MOD - hsh[l-1]));
}
void solve(){
string s;
cin >> s;
int n = s.size();
if(n == 1){
cout << 1 << "\n";
return;
}
powers[0] = 1;
for(int i=1; i<=n; i++) powers[i] = mul(p, powers[i-1]);
hsh[0] = 1 + (s[0] - 'a');
for(int i=1; i<n; i++){
hsh[i] = add(mul(p, hsh[i-1]), (s[i]-'a') + 1);
}
int l1=0, r1=0, l2=n-1, r2=n-1;
int res = 0;
while(1){
//cout << l1 << " " << r1 << " " << l2 << " " << r2 << endl;
//cout << substring_hash(l1, r1) << " " << substring_hash(l2, r2) << endl;
if(substring_hash(l1, r1) == substring_hash(l2, r2)){
res += 2;
r2 = l2-1;
l1 = r1+1;
r1++;
l2--;
if(r1 > l2){
cout << res << "\n";
return;
}
if(r1 == l2){
cout << res+1 << "\n";
return;
}
}
else{
r1++;
l2--;
if(r1 >= l2){
cout << res+1 << "\n";
return;
}
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cout.precision(10);
cout << fixed;
int t;
cin >> t;
while(t--){
solve();
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
7 ms |
348 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
7 ms |
348 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
9 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
7 ms |
348 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
9 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
9 ms |
632 KB |
Output is correct |
11 |
Correct |
4 ms |
504 KB |
Output is correct |
12 |
Correct |
5 ms |
504 KB |
Output is correct |
13 |
Correct |
5 ms |
504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
7 ms |
348 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
9 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
9 ms |
632 KB |
Output is correct |
11 |
Correct |
4 ms |
504 KB |
Output is correct |
12 |
Correct |
5 ms |
504 KB |
Output is correct |
13 |
Correct |
5 ms |
504 KB |
Output is correct |
14 |
Correct |
283 ms |
20868 KB |
Output is correct |
15 |
Correct |
154 ms |
15800 KB |
Output is correct |
16 |
Correct |
280 ms |
19980 KB |
Output is correct |
17 |
Correct |
158 ms |
11220 KB |
Output is correct |