Submission #140269

# Submission time Handle Problem Language Result Execution time Memory
140269 2019-08-02T12:14:06 Z ae04071 Necklace (Subtask 1-3) (BOI19_necklace1) C++11
85 / 85
391 ms 508 KB
#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

necklace.cpp: In function 'int main()':
necklace.cpp:63:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%s%s",s,t);
     ~~~~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 3 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 3 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 8 ms 376 KB Output is correct
7 Correct 6 ms 376 KB Output is correct
8 Correct 8 ms 376 KB Output is correct
9 Correct 12 ms 428 KB Output is correct
10 Correct 6 ms 376 KB Output is correct
11 Correct 6 ms 376 KB Output is correct
12 Correct 7 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 3 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 8 ms 376 KB Output is correct
7 Correct 6 ms 376 KB Output is correct
8 Correct 8 ms 376 KB Output is correct
9 Correct 12 ms 428 KB Output is correct
10 Correct 6 ms 376 KB Output is correct
11 Correct 6 ms 376 KB Output is correct
12 Correct 7 ms 376 KB Output is correct
13 Correct 322 ms 404 KB Output is correct
14 Correct 258 ms 376 KB Output is correct
15 Correct 391 ms 476 KB Output is correct
16 Correct 296 ms 376 KB Output is correct
17 Correct 202 ms 504 KB Output is correct
18 Correct 215 ms 376 KB Output is correct
19 Correct 237 ms 436 KB Output is correct
20 Correct 296 ms 504 KB Output is correct
21 Correct 313 ms 508 KB Output is correct