# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
367369 | GusMG | Necklace (Subtask 4) (BOI19_necklace4) | C++17 | 156 ms | 65540 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;
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define per(i,a,b) for(int i=(b)-1;i>=(a);i--)
using ll=long long;
using vi=vector<int>;
//pi[i] = longitud del prefijo propio mas grande de s[0..i] que tambien es sufijo
vi prefix_function(string s) {
vi pi(s.size());
for(int i=1,j=0; i<s.size(); i++) {
while(j && s[i]!=s[j]) j=pi[j-1];
if(s[i]==s[j]) j++;
pi[i] = j;
}
return pi;
}
// Busca las coincidencias de p en s, regresa indices de inicio
vi kmp(string s, string p) {
vi pi = prefix_function(p);
vi ans(s.size());
for(int i=0,j=0; i<s.size(); i++) {
while(j && s[i]!=p[j]) j=pi[j-1];
if(s[i]==p[j]) j++;
ans[i]=j;
if(j==p.size()) {
j=pi[j-1];
}
}
return ans;
}
int main(){
ios_base::sync_with_stdio(0);
string s,t;
cin>>s>>t;
int n=s.size(),m=t.size(),ans=0,l=0,r=0;
vector<vi> lps(m),lsp(n);
rep(k,0,m) {
string p=t.substr(k);
lps[k]=kmp(s,p);
}
rep(k,0,n) {
string p=s.substr(k);
lsp[k]=kmp(t,p);
}
rep(i,0,n-1) rep(j,0,m-1) {
int a=lps[j+1][i],b=lsp[i+1][j];
if(ans<a+b) ans=a+b,l=i-a+1,r=j-b+1;
}
reverse(s.begin(),s.end());
rep(k,0,m) {
string p=t.substr(k);
lps[k]=kmp(s,p);
}
rep(k,0,n) {
string p=s.substr(k);
lsp[k]=kmp(t,p);
}
rep(i,0,n-1) rep(j,0,m-1) {
int a=lps[j+1][i],b=lsp[i+1][j];
if(ans<a+b) ans=a+b,l=n-i-a-1,r=j-b+1;
}
cout<<ans<<"\n";
cout<<l<<" "<<r<<"\n";
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |