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;
string s,ss;
const int maxn=3000+10;
int lcp[maxn][maxn],fkmp[maxn][maxn];
int n,m;
void findlcp(){
for(int i=0;i<maxn;i++){
for(int j=0;j<maxn;j++){
lcp[i][j]=0;
}
}
for(int i=n-1;i>=0;i--){
for(int j=m-1;j>=0;j--){
if(s[i]==ss[j]){
lcp[i][j]=lcp[i+1][j+1]+1;
}
}
}
}
void findkmp(){
for(int i=0;i<maxn;i++){
for(int j=0;j<maxn;j++){
fkmp[i][j]=0;
}
}
for(int i=0;i<n;i++){
string fake;
for(int j=i;j<n;j++)
{
fake.push_back(s[j]);
}
fake.push_back('$');
fake+=ss;
int len=n-i;
// if(i==5){
// cout<<fake<<"\n";
// }
vector<int>kmp(fake.size()+10);
kmp[0]=0;
for(int j=1;j<(int)fake.size();j++){
int now=kmp[j-1];
//int cnt=0;
while(now>=0){
//cnt++;
if(fake[j]==fake[now]){
break;
}
if(now==0){
now=-1;
break;
}
now=kmp[now-1];
}
now++;
kmp[j]=now;
if(j>len){
fkmp[i][j-len-1]=now;
}
// cout<<len<<" "<<j<<" "<<kmp[j]<<"\n";
}
}
}
pair<int,pair<int,int>> solve(){
n=s.size(),m=ss.size();
findlcp();
findkmp();
//cout<<lcp[3][4]<<" "<<maxa[3][5]<<"\n";
pair<int,pair<int,int>>res;
res.first=0;
res.second=make_pair(0,0);
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(lcp[i][j]>=1){
res=max(res,make_pair(lcp[i][j],make_pair(i,j)));
if(j==0){
continue;
}
int lasta=i+lcp[i][j]+fkmp[i+lcp[i][j]][j-1]-1;
// if(i==3&&j==4){
// cout<<lasta<<"\n";
// }
res=max(res,make_pair(lasta-i+1,make_pair(i,j-1-(lasta-(i+lcp[i][j]-1))+1)));
}
}
}
return res;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>s>>ss;
auto res=solve();
reverse(ss.begin(),ss.end());
auto res2=solve();
//cout<<res2.second.second<<"\n";
if(res2.first>res.first){
swap(res2,res);
res.second.second=m-(res.second.second+res.first-1)-1;
}
cout<<res.first<<"\n";
cout<<res.second.first<<" "<<res.second.second<<"\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |