Submission #716699

#TimeUsernameProblemLanguageResultExecution timeMemory
716699amirhoseinfar1385Necklace (Subtask 1-3) (BOI19_necklace1)C++17
85 / 85
263 ms71272 KiB
#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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...