# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
367374 | GusMG | Necklace (Subtask 4) (BOI19_necklace4) | C++17 | 249 ms | 748 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;
string s2=s,t2=t;
reverse(s2.begin(),s2.end());
reverse(t2.begin(),t2.end());
rep(i,1,n) {
vi lps=kmp(t,s.substr(i)),lsp=kmp(t2,s2.substr(n-i));
rep(j,0,m-1) {
int a=lps[j],b=lsp[m-j-2];
if(ans<a+b) ans=a+b,l=i-b,r=j-a+1;
}
lps=kmp(t,s2.substr(i)),lsp=kmp(t2,s.substr(n-i));
rep(j,0,m-1) {
int a=lps[j],b=lsp[m-j-2];
if(ans<a+b) ans=a+b,l=n-i-a,r=j-a+1;
}
}
cout<<ans<<"\n";
cout<<l<<" "<<r<<"\n";
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |