#include <bits/stdc++.h>
#define pb push_back
#define X first
#define Y second
#define pii pair<int, int>
using namespace std;
int mod = 1e9+7;
int omod = 1e9+9;
vector<int> mem, omem;
bool same(int l, int r, int len, vector<int>&wrd){
for(int i = 0; i<=len; i++){
if(wrd[l+i] != wrd[r+i-len])return false;
}
return true;
}
int dp(int l, int r, vector<int>&wrd){
if(r<l)return 0;
int hsh1, hsh2, hsh3, hsh4;hsh1 = 0; hsh2=0;
hsh3 = 0; hsh4 = 0;
for(int i = 0; i+l < r-i ;i++){
hsh1*=31; hsh1+= wrd[l+i];
hsh3*=31; hsh3+= wrd[l+i];
hsh2+= wrd[r-i]*mem[i];
hsh4+= wrd[r-i]*omem[i];
hsh1%=mod; hsh2%=mod; hsh3%=omod; hsh4%=omod;
if(hsh1 == hsh2 && hsh3 == hsh4 && same(l, r, i, wrd))return dp(l+i+1, r-i-1, wrd)+2;
}
return 1;
}
void solve(){
string in; vector<int> wrd;
cin >> in;
for(auto i : in)wrd.pb(i-'a');
//for(int i : wrd)cout << i <<' ';
cout << dp(0, wrd.size()-1, wrd)<< '\n';
}
void prec(){
int pw = 1;
int opw = 1;
for(int i = 0; i< 1e6; i++){
mem.pb(pw);
omem.pb(opw);
pw*=31;
opw*=31;
if(pw>mod)pw-=mod;
if(opw>omod)opw-=omod;
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int t; cin >> t;
prec();
while(t--){
solve();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
20 ms |
8412 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
20 ms |
8412 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
20 ms |
8412 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
20 ms |
8412 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |