#include <bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define all(a) a.begin(), a.end()
#define int long long
const int mod = 1e9 + 7;
const int p = 31;
const int N = 2e6;
/*
4
bonobo
deleted
racecar
racecars
*/
int pw[N+10];
void solve(){
string s; cin >> s;
int n = s.size();
vector<int> hash(n, 0);
hash[0] = s[0];
for(int i = 1;i < n; i++){
hash[i] = (hash[i-1] * p + s[i]) % mod;
}
auto get=[&](int l, int r){
if(l == 0) return hash[r];
int sum = hash[r] - (pw[r-l+1] * hash[l-1]);
sum = ((sum % mod) + mod) % mod;
return sum;
};
deque<char> dq;
for(int i = 0;i < n; i++) dq.push_back(s[i]);
int left = 0, right = n-1;
int ans = 0;
while(true){
int len = -1;
for(int j = right; j >= 0; j--){
if(left + right - j >= j) break;
/*cout << j+1 << " " << right + 1 << '\n';
cout << left + 1 << ' ' << left + right - j + 1 << "\n";
cout << get(j, right) << ' ' << get(left, left+(right-j)) << '\n';
* */
if(get(j, right) == get(left, left+(right-j))){
len = (right-j+1);
break;
}
}
if(len == -1){
ans++;
break;
}
ans+= 2;
if(dq.empty()) break;
left+= len, right-= len;
if(right < left) break;
}
cout << ans;
}
signed main(){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
pw[0] = 1;
for(int i = 1;i <= N; i++){
pw[i] = (pw[i-1] * p) % mod;
}
int tt; cin >> tt;
while(tt--){
solve();
cout << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
15964 KB |
Output is correct |
2 |
Correct |
11 ms |
16000 KB |
Output is correct |
3 |
Correct |
10 ms |
15960 KB |
Output is correct |
4 |
Correct |
10 ms |
15960 KB |
Output is correct |
5 |
Correct |
11 ms |
15964 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
15964 KB |
Output is correct |
2 |
Correct |
11 ms |
16000 KB |
Output is correct |
3 |
Correct |
10 ms |
15960 KB |
Output is correct |
4 |
Correct |
10 ms |
15960 KB |
Output is correct |
5 |
Correct |
11 ms |
15964 KB |
Output is correct |
6 |
Correct |
10 ms |
15964 KB |
Output is correct |
7 |
Correct |
10 ms |
15964 KB |
Output is correct |
8 |
Correct |
11 ms |
15964 KB |
Output is correct |
9 |
Correct |
11 ms |
15964 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
15964 KB |
Output is correct |
2 |
Correct |
11 ms |
16000 KB |
Output is correct |
3 |
Correct |
10 ms |
15960 KB |
Output is correct |
4 |
Correct |
10 ms |
15960 KB |
Output is correct |
5 |
Correct |
11 ms |
15964 KB |
Output is correct |
6 |
Correct |
10 ms |
15964 KB |
Output is correct |
7 |
Correct |
10 ms |
15964 KB |
Output is correct |
8 |
Correct |
11 ms |
15964 KB |
Output is correct |
9 |
Correct |
11 ms |
15964 KB |
Output is correct |
10 |
Correct |
12 ms |
16220 KB |
Output is correct |
11 |
Correct |
11 ms |
16220 KB |
Output is correct |
12 |
Correct |
12 ms |
16300 KB |
Output is correct |
13 |
Correct |
12 ms |
16220 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
15964 KB |
Output is correct |
2 |
Correct |
11 ms |
16000 KB |
Output is correct |
3 |
Correct |
10 ms |
15960 KB |
Output is correct |
4 |
Correct |
10 ms |
15960 KB |
Output is correct |
5 |
Correct |
11 ms |
15964 KB |
Output is correct |
6 |
Correct |
10 ms |
15964 KB |
Output is correct |
7 |
Correct |
10 ms |
15964 KB |
Output is correct |
8 |
Correct |
11 ms |
15964 KB |
Output is correct |
9 |
Correct |
11 ms |
15964 KB |
Output is correct |
10 |
Correct |
12 ms |
16220 KB |
Output is correct |
11 |
Correct |
11 ms |
16220 KB |
Output is correct |
12 |
Correct |
12 ms |
16300 KB |
Output is correct |
13 |
Correct |
12 ms |
16220 KB |
Output is correct |
14 |
Correct |
147 ms |
36064 KB |
Output is correct |
15 |
Correct |
78 ms |
30808 KB |
Output is correct |
16 |
Correct |
132 ms |
33588 KB |
Output is correct |
17 |
Correct |
79 ms |
26384 KB |
Output is correct |