Submission #714750

#TimeUsernameProblemLanguageResultExecution timeMemory
714750amirhoseinfar1385Necklace (Subtask 1-3) (BOI19_necklace1)C++17
5 / 85
1557 ms1908 KiB
#include<bits/stdc++.h> using namespace std; const int maxn=10000; int n,m; int base=29,mod=923478557,base2=31,mod2=1e9+7; int ww(int a){ if(a>=mod){ a-=mod; } return a; } int ww2(int a){ if(a>=mod){ a-=mod; } return a; } int mp[maxn],revmp[maxn],mp2[maxn],revmp2[maxn]; long long mypow(long long m,long long y){ if(y==0){ return 1; } long long p=mypow(m,(y>>1)); p*=p; p%=mod; if(y&1){ p*=m; p%=mod; } return p; } long long mypow2(long long m,long long y){ if(y==0){ return 1; } long long p=mypow(m,(y>>1)); p*=p; p%=mod; if(y&1){ p*=m; p%=mod; } return p; } int main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); mp[0]=1; mp2[0]=1; for(int i=1;i<maxn;i++){ mp[i]=1ll*mp[i-1]*base%mod; mp2[i]=1ll*mp2[i-1]*base2%mod; } revmp[maxn-1]=mypow(mp[maxn-1],mod-2); revmp2[maxn-1]=mypow2(mp2[maxn-1],mod-2); for(int i=maxn-2;i>=0;i--){ revmp[i]=1ll*revmp[i+1]*base%mod; revmp2[i]=1ll*revmp2[i+1]*base2%mod; } string s,ss; cin>>s>>ss; n=s.size(); m=ss.size(); vector<int>hashn(n+2),revhashn(n+2),hashm(m+2),revhashm(m+2); vector<int>hashn2(n+2),revhashn2(n+2),hashm2(m+2),revhashm2(m+2); for(int i=1;i<=n;i++){ hashn[i]=(1ll*mp[i-1]*s[i-1]+hashn[i-1])%mod; hashn2[i]=(1ll*mp2[i-1]*s[i-1]+hashn2[i-1])%mod; } for(int i=n;i>0;i--){ revhashn[i]=(1ll*mp[n-i]*s[i-1]+revhashn[i+1])%mod; revhashn2[i]=(1ll*mp2[n-i]*s[i-1]+revhashn2[i+1])%mod; } for(int i=1;i<=m;i++){ hashm[i]=(1ll*ss[i-1]*mp[i-1]+hashm[i-1])%mod; hashm2[i]=(1ll*ss[i-1]*mp2[i-1]+hashm2[i-1])%mod; } for(int i=m;i>0;i--){ revhashm[i]=(1ll*mp[m-i]*ss[i-1]+revhashm[i+1])%mod; revhashm2[i]=(1ll*mp2[m-i]*ss[i-1]+revhashm2[i+1])%mod; } vector<pair<pair<int,int>,int>>allh[n+1]; for(int i=1;i<=n;i++){ for(int j=i;j<=n;j++){ int fake=mod+hashn[j]-hashn[i-1]; int fake2=mod+hashn2[j]-hashn2[i-1]; fake=ww(fake); fake2=ww2(fake2); fake2=1ll*fake2*revmp2[i-1]%mod; fake=1ll*fake*revmp[i-1]%mod; allh[j-i+1].push_back(make_pair(make_pair(fake,fake2),i)); // if(i==4&&j==n){ // cout<<fake2<<" 1"<<endl; // } } } for(int i=0;i<=n;i++){ sort(allh[i].begin(),allh[i].end()); allh[i].resize(unique(allh[i].begin(),allh[i].end())-allh[i].begin()); } pair<int,pair<int,int>>res; res.first=-1; for(int i=1;i<=m;i++){ for(int j=i;j<=m;j++){ if(j-i+1>n||j-i+1<=res.first){ continue; } for(int h=i;h<=j;h++){ int fake=mod+hashm[j]-hashm[h-1]; int fake2=mod+hashm2[j]-hashm2[h-1]; fake=ww(fake); fake2=ww2(fake2); fake=1ll*fake*revmp[h-1]%mod; fake2=1ll*fake2*revmp2[h-1]%mod; fake+=1ll*(hashm[h]-hashm[i-1]+mod)*revmp[i-1]%mod*mp[j-h+1]%mod; fake2+=1ll*(hashm2[h]-hashm2[i-1]+mod)*revmp2[i-1]%mod*mp2[j-h+1]%mod; fake=ww(fake); fake2=ww(fake2); int p=lower_bound(allh[j-i+1].begin(),allh[j-i+1].end(),make_pair(make_pair(fake,fake2),-1))-allh[j-i+1].begin(); if(p!=allh[j-i+1].size()&&allh[j-i+1][p].first==make_pair(fake,fake2)){ res=max(res,make_pair(j-i+1,make_pair(i,allh[j-i+1][p].second))); } fake=mod+revhashm[i]-revhashm[h+1]; fake2=mod+revhashm2[i]-revhashm2[h+1]; // if(i==3&&j==6&&h==4){ // cout<<fake<<" 2 "<<endl; // } fake=ww(fake); fake2=ww2(fake2); fake=1ll*fake*revmp[m-h]%mod; fake2=1ll*fake2*revmp2[m-h]%mod; int z=1ll*(mod+revhashm[h+1]-revhashm[j+1])*revmp[m-j]%mod*mp[h-i+1]%mod; int z2=1ll*(mod+revhashm2[h+1]-revhashm2[j+1])*revmp2[m-j]%mod*mp2[h-i+1]%mod; fake+=z; fake2+=z2; fake2=ww(fake2); fake=ww(fake); // if(i==3&&j==6&&h==4){ // cout<<fake<<" 2 "<<z<<endl; // } p=lower_bound(allh[j-i+1].begin(),allh[j-i+1].end(),make_pair(make_pair(fake,fake2),-1))-allh[j-i+1].begin(); if(p!=allh[j-i+1].size()&&allh[j-i+1][p].first==make_pair(fake,fake2)){ res=max(res,make_pair(j-i+1,make_pair(i,allh[j-i+1][p].second))); } } } } if(res.first==-1){ cout<<-1<<"\n"; return 0; } cout<<res.first<<"\n"; cout<<res.second.second-1<<" "<<res.second.first-1<<"\n"; }

Compilation message (stderr)

necklace.cpp: In function 'int main()':
necklace.cpp:121:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<std::pair<int, int>, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  121 |         if(p!=allh[j-i+1].size()&&allh[j-i+1][p].first==make_pair(fake,fake2)){
      |            ~^~~~~~~~~~~~~~~~~~~~
necklace.cpp:143:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<std::pair<int, int>, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  143 |         if(p!=allh[j-i+1].size()&&allh[j-i+1][p].first==make_pair(fake,fake2)){
      |            ~^~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...