Submission #99304

#TimeUsernameProblemLanguageResultExecution timeMemory
99304dsg213Palinilap (COI16_palinilap)C++14
100 / 100
163 ms41508 KiB
#include<bits/stdc++.h> using namespace std; #define pb push_back #define mp make_pair #define st first #define nd second #define ll long long #define ull unsigned ll #define pii pair<int,int> #define pll pair<ll,ll> #define vi vector<int> const ll mod=1000000007,mxn=1000010; string s; int n; ull p=313; ull hap[mxn]; ull hal[mxn]; ull pot[mxn]; ll hap2[mxn]; ll hal2[mxn]; ll p2=2137; ll pot2[mxn]; pair<ull,ll> gethp(int left,int right){ return mp(hap[right]-hap[left-1]*pot[right-left+1], ((hap2[right]-hap2[left-1]*pot2[right-left+1])%mod+mod)%mod ); } pair<ull,ll> gethl(int left,int right){ return mp(hal[left]-hal[right+1]*pot[right-left+1] , ((hal2[left]-hal2[right+1]*pot2[right-left+1])%mod+mod)%mod ) ; } ll acc[mxn]; ll ilz[mxn]; ll wie[mxn][26]; ll ans; void liczsro(int x){ int mn=1; int mx=min(x,n-x+1); ////cout <<mx <<" "; while(mn<mx){ int sr=(mn+mx+1)/2; if(gethp(x-sr+1,x)==gethl(x,x+sr-1)){ mn=sr; } else{ mx=sr-1; } } ////cout <<mn <<" "; acc[x-mn]+=1; acc[x]-=2; acc[x+mn]+=1; ilz[x]-=mn; int baz=mn; ans+=baz; if(baz==min(x,n-x+1)){ ////cout <<"0\n"; return; } mx=min(x,n-x+1); mn=baz+1; while(mn<mx){ int sr=(mn+mx+1)/2; if(gethp(x-sr+1,x-baz-1) == gethl(x+baz+1,x+sr-1)){ mn=sr; } else{ mx=sr-1; } } int dod=mn-baz; ////cout <<baz <<" "<<dod <<"\n"; char c1=s[x-baz-1]-'a'; char c2=s[x+baz-1]-'a'; wie[x-baz][c2]+=dod; wie[x+baz][c1]+=dod; ////cout <<x <<" " <<baz <<" " <<c2+'a' <<"\n"; } void liczd(int x){ int mn=0; int mx=min(x,n-x); while(mn<mx){ int sr=(mn+mx+1)/2; if(gethp(x-sr+1,x)==gethl(x+1,x+sr)){ mn=sr; } else{ mx=sr-1; } } ////cout <<mn <<" "; acc[x-mn]+=1; acc[x]-=1; acc[x+1]-=1; acc[x+mn+1]+=1; //ilz[x]-=mn; int baz=mn; ans+=baz; if(baz==min(x,n-x)){ ////cout <<"0 "; return; } mx=min(x,n-x); mn=baz+1; ////cout <<mx <<" "; while(mn<mx){ int sr=(mn+mx+1)/2; if(gethp(x-sr+1,x-baz-1) == gethl(x+baz+2,x+sr)){ mn=sr; } else{ mx=sr-1; } } ll dod=mn-baz; ////cout <<dod <<" "; char c1=s[x-baz-1]-'a'; char c2=s[x+baz]-'a'; wie[x-baz][c2]+=dod; wie[x+baz+1][c1]+=dod; ////cout <<x-baz <<" " <<'a'+c2 <<"\n"; } void pro(){ ll pr=0; ll val=0; for(int i=0;i<=n;i++){ ilz[i]+=val; pr+=acc[i]; val+=pr; } } int main(){ cin >>s; n=s.size(); pot[0]=1; pot2[0]=1; for(ull i=1;i<=mxn-10;i++){ pot[i]=pot[i-1]*p; pot2[i]=pot2[i-1]*p2%mod; } for(int i=0;i<n;i++){ hap[i+1]=hap[i]*p+s[i]; hap2[i+1]=(hap2[i]*p2+s[i])%mod; } for(int i=n;i>0;i--){ hal[i]=hal[i+1]*p+s[i-1]; hal2[i]=(hal2[i+1]*p2+s[i-1])%mod; } /*while(1){ int a,b; cin >>a >>b; //cout <<gethp(a,b) <<"\n"; //cout <<gethl(a,b) <<"\n"; }*/ for(int i=1;i<=n;i++){ liczsro(i); } ////cout <<"\n"; for(int i=1;i<n;i++){ liczd(i); } ////cout <<"\n"; pro(); ////cout <<"\n"; /*for(int i=0;i<=n;i++){ //cout <<acc[i] <<" "; } //cout <<"\n"; */ /*for(int i=0;i<=n;i++){ //cout <<ilz[i] <<" "; }*/ //cout <<ans<<"\n"; ll wyn=ans; for(int i=1;i<=n;i++){ //cout <<ilz[i] <<" "; for(int j=0;j<26;j++){ wyn=max(wyn,ans+wie[i][j]-ilz[i]); } } //cout <<"\n"; for(int i=1;i<=n;i++){ ll x=0; for(int j=0;j<26;j++){ x=max(x,wie[i][j]); } //cout <<x<<" "; } //cout <<"\n"; cout <<wyn; }

Compilation message (stderr)

palinilap.cpp: In function 'void liczsro(int)':
palinilap.cpp:72:15: warning: array subscript has type 'char' [-Wchar-subscripts]
  wie[x-baz][c2]+=dod;
               ^
palinilap.cpp:73:15: warning: array subscript has type 'char' [-Wchar-subscripts]
  wie[x+baz][c1]+=dod;
               ^
palinilap.cpp: In function 'void liczd(int)':
palinilap.cpp:116:15: warning: array subscript has type 'char' [-Wchar-subscripts]
  wie[x-baz][c2]+=dod;
               ^
palinilap.cpp:117:17: warning: array subscript has type 'char' [-Wchar-subscripts]
  wie[x+baz+1][c1]+=dod;
                 ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...