Submission #499722

#TimeUsernameProblemLanguageResultExecution timeMemory
499722codr0Palindromic Partitions (CEOI17_palindromic)C++14
100 / 100
2058 ms28580 KiB
#include <bits/stdc++.h> #define FOR(i,a,b) for(int i=a;i<=b;i++) #define FORR(i,a,b) for(int i=a;i>=b;i--) #define S second #define F first #define pb push_back #define SZ(x) (int)x.size() #define all(x) x.begin(),x.end() #define err(x) cout<<#x<<": "<<x<<'\n'; #define int long long using namespace std; const int MOD=98765431; const int M=31; int pw(int a,int b){ if(!b) return 1; int rt=pw(a,b/2); rt=(rt*rt)%MOD; if(b&1) rt=(rt*a)%MOD; return rt; } int inv(int x){ return pw(x,MOD-2); } void Main(){ int n; string s; cin>>s; n=SZ(s); s='$'+s; int ps[n+1]={}; int PW[n+1]; PW[0]=1; FOR(i,1,n) PW[i]=(PW[i-1]*M)%MOD; FOR(i,1,n){ ps[i]=ps[i-1]; ps[i]=(ps[i]+(((s[i]-'a'+1)*PW[i-1])%MOD))%MOD; } int l=1,r=n; int ans=0; while(r>l){ int mid1=(r+l)/2; int mid2=mid1+1; if((r-l+1)&1) mid1--; int t=-1; FOR(i,0,mid1-l){ int hsh1=ps[i+l]-ps[l-1]; hsh1=hsh1*inv(PW[l-1]); hsh1=((hsh1%MOD)+MOD)%MOD; int hsh2=ps[r]-ps[r-i-1]; hsh2=hsh2*inv(PW[r-i-1]); hsh2=((hsh2%MOD)+MOD)%MOD; if(hsh1==hsh2) {t=i; break;} } if(t==-1) { ans++; break; }else{ l=l+t+1; r=r-t-1; ans+=2; } } if(r==l) ans++; cout<<ans<<'\n'; } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); int T; cin>>T; while(T--) Main(); return 0; }

Compilation message (stderr)

palindromic.cpp: In function 'void Main()':
palindromic.cpp:43:8: warning: unused variable 'mid2' [-Wunused-variable]
   43 |    int mid2=mid1+1;
      |        ^~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...