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>
#define pb push_back
#define X first
#define Y second
#define pii pair<int, int>
#define int int64_t
#define pow pww
using namespace std;
int mod = 1e9+7;
vector<int>hsh, pow;
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 hval(int l, int r){
int ans;
r++;
ans = hsh[r] - hsh[l]*pow[r-l+1];
ans %= mod;
if(ans<0)ans+=mod;
return ans;
}
int dp(int l, int r, vector<int>&wrd){
if(r<l)return 0;
for(int i = 0; i+l < r-i ;i++){
if(hval(l, l+i) == hval(r-i, r) && same(l, r, i, wrd))return dp(l+i+1, r-i-1, wrd)+2;
}
return 1;
}
void solve(){
hsh.clear(); pow.clear();
string in; vector<int> wrd;
cin >> in;
for(auto i : in)wrd.pb(i-'a');
pow.pb(0); hsh.pb(0); pow.pb(1);
for(int i = 0; i < wrd.size(); i++){
hsh.pb(hsh.back()*31+wrd[i]);
hsh.back()%=mod;
pow.pb(pow.back()*31);
}
//for(int i : wrd)cout << i <<' ';
cout << dp(0, wrd.size()-1, wrd)<< '\n';
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int t; cin >> t;
while(t--){
solve();
}
return 0;
}
Compilation message (stderr)
palindromic.cpp: In function 'void solve()':
palindromic.cpp:39:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < wrd.size(); i++){
~~^~~~~~~~~~~~
# | 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... |