# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
714785 | amirhoseinfar1385 | Necklace (Subtask 1-3) (BOI19_necklace1) | C++17 | 0 ms | 0 KiB |
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>
using namespace std;
const int maxn=10000;
int n,m;
const int base=29,mod=43691,base2=31,mod2=68239;
int vis[mod][mod2];
int ww(int a){
if(a>=mod){
a-=mod;
}
return a;
}
int ww2(int a){
if(a>=mod2){
a-=mod2;
}
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%=mod2;
if(y&1){
p*=m;
p%=mod2;
}
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%mod2;
}
revmp[maxn-1]=mypow(mp[maxn-1],mod-2);
revmp2[maxn-1]=mypow2(mp2[maxn-1],mod2-2);
for(int i=maxn-2;i>=0;i--){
revmp[i]=1ll*revmp[i+1]*base%mod;
revmp2[i]=1ll*revmp2[i+1]*base2%mod2;
}
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])%mod2;
}
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])%mod2;
}
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])%mod2;
}
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])%mod2;
}
for(int i=1;i<=n;i++){
}
int cnt=0;
pair<int,pair<int,int>>res;
res.first=-1;
for(int fi=1;fi<=min(m,n);fi++,cnt++){
for(int j=fi;j<=n;j++){
int i=j-fi+1;
int fake=mod+hashn[j]-hashn[i-1];
int fake2=mod2+hashn2[j]-hashn2[i-1];
fake=ww(fake);
fake2=ww2(fake2);
fake2=1ll*fake2*revmp2[i-1]%mod2;
fake=1ll*fake*revmp[i-1]%mod;
vis[fake][fake2]=cnt*n+i;
// if(i==4&&j==n){
// cout<<fake2<<" 1"<<endl;
// }
}
for(int j=fi;j<=m;j++){
int i=j-fi+1;
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=mod2+hashm2[j]-hashm2[h-1];
fake=ww(fake);
fake2=ww2(fake2);
fake=1ll*fake*revmp[h-1]%mod;
fake2=1ll*fake2*revmp2[h-1]%mod2;
fake+=1ll*(hashm[h]-hashm[i-1]+mod)*revmp[i-1]%mod*mp[j-h+1]%mod;
fake2+=1ll*(hashm2[h]-hashm2[i-1]+mod2)*revmp2[i-1]%mod2*mp2[j-h+1]%mod2;
fake=ww(fake);
fake2=ww(fake2);
if(vis[fake][fake2]>=cnt*n){
res=make_pair(j-i+1,make_pair(i,vis[fake][fake2]));
}
fake=mod+revhashm[i]-revhashm[h+1];
fake2=mod2+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]%mod2;
int z=1ll*(mod+revhashm[h+1]-revhashm[j+1])*revmp[m-j]%mod*mp[h-i+1]%mod;
int z2=1ll*(mod2+revhashm2[h+1]-revhashm2[j+1])*revmp2[m-j]%mod2*mp2[h-i+1]%mod2;
fake+=z;
fake2+=z2;
fake2=ww(fake2);
fake=ww(fake);
// if(i==3&&j==6&&h==4){
// cout<<fake<<" 2 "<<z<<endl;
// }
if(vis[fake][fake2]>=cnt*n){
res=make_pair(j-i+1,make_pair(i,vis[fake][fake2]-cnt*n));
}
}
}
}
if(res.first==-1){
cout<<-1<<"\n";
return 0;
}
cout<<res.first<<"\n";
cout<<res.second.second-1<<" "<<res.second.first-1<<"\n";
}