# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
140271 | ae04071 | Necklace (Subtask 4) (BOI19_necklace4) | C++11 | 393 ms | 632 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;
int n,m;
char s[3010],t[3010];
int pi[3010],fm[3010],bm[3010];
void getpi() {
int i=0;
for(int j=1;j<m;j++) {
while(i && t[i]!=t[j]) i = pi[i-1];
if(t[i]==t[j]) pi[j]=++i;
}
}
void kmp(int *arr) {
int j=0;
for(int i=0;i<n;i++) {
while(j && s[i]!=t[j]) {
j = pi[j-1];
}
if(s[i]==t[j]) {
++j;
if(j==m) {
j=pi[j-1];
}
}
arr[i] = j;
}
}
int ans=0,ai,aj,f;
void solve() {
for(int i=0;i<m;i++) {
getpi();
kmp(fm);
reverse(s, s+n);
reverse(t, t+m);
getpi();
kmp(bm);
reverse(s, s+n);
reverse(t,t+m);
reverse(bm, bm+n);
for(int j=0;j<n-1;j++) {
int v = min(fm[j],m-i) + min(bm[j+1],i);
if(ans < v) {
ans = v;
ai = j - fm[j] + 1;
if(!f) aj = i - min(bm[j+1], i);
else aj = m-i - min(fm[j], m-i);
}
}
char ch = t[0];
for(int j=0;j<m-1;j++) t[j]=t[j+1];
t[m-1]=ch;
}
}
int main() {
scanf("%s%s",s,t);
n=strlen(s);
m=strlen(t);
solve();
f=1;
reverse(t,t+m);
solve();
printf("%d\n%d %d\n",ans,ai,aj);
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |