Submission #854906

# Submission time Handle Problem Language Result Execution time Memory
854906 2023-09-29T10:24:16 Z aymanrs Necklace (Subtask 1-3) (BOI19_necklace1) C++14
25 / 85
284 ms 225600 KB
#include<bits/stdc++.h>
using namespace std;
struct node {
    node* nx[26]={NULL};
    short st=-1;
};
void del(node*r){
    if(!r)return;
    for(int i = 0;i < 26;i++) del(r->nx[i]);
    delete r;
}
char S[10000], F[10000];
int N;
int leastrot(){
    fill(F, F+N, -1);
    int k = 0;
    for(int i = 1; i < N; i++){
        char c = S[i];
        int f = F[i-k-1];
        while(f != -1 && c != S[k+f+1]){
            if(c < S[k+f+1])
                k = i-f-1;
            f = F[f];
        }

        if(c != S[k+f+1]){
            if(c < S[k])
                k = i;
            F[i-k] = -1;
        } else F[i-k] = f+1;
    }
    return k;
}
void add(node* r, int l){
    int f = leastrot();
    for(int i = 0;i < (N>>1);i++){
        S[f+i]-='a';
        if(!r->nx[S[f+i]]) r->nx[S[f+i]] = new node;
        r = r->nx[S[f+i]];
    }
    r->st = l;
}
short fi(node* r){
    int f = leastrot();
    for(int i = 0;i < (N>>1);i++){
        S[f+i]-='a';
        if(!r->nx[S[f+i]]) return -1;
        r = r->nx[S[f+i]];
    }
    return r->st;
} 
void solve(){
    string a, b;cin >> a >> b;
    int n = a.size(), m = b.size();
    node* r = new node;
    for(int i = 0;i < n;i++) for(int j = 1;i+j <= n;j++) {
        N = j<<1;
        for(int k = 0;k < j;k++) S[k] = S[k+j] = a[i+k];
        add(r, i);
    }
    reverse(a.begin(), a.end());
    for(int i = 0;i < n;i++) for(int j = 1;i+j <= n;j++) {
        N = j<<1;
        for(int k = 0;k < j;k++) S[k] = S[k+j] = a[i+k];
        add(r, n-i-j);
    }
    for(int j = m;j >= 1;j--) for(int i = 0;i+j <= m;i++){
        N = j<<1;
        for(int k = 0;k < j;k++) S[k] = S[k+j] = b[i+k];
        int f = fi(r);
        if(f != -1){
            cout << j << '\n' << f << ' ' << i << '\n';
            return;
        }
    }
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    solve();
}

Compilation message

necklace.cpp: In function 'void add(node*, int)':
necklace.cpp:38:24: warning: array subscript has type 'char' [-Wchar-subscripts]
   38 |         if(!r->nx[S[f+i]]) r->nx[S[f+i]] = new node;
      |                   ~~~~~^
necklace.cpp:38:39: warning: array subscript has type 'char' [-Wchar-subscripts]
   38 |         if(!r->nx[S[f+i]]) r->nx[S[f+i]] = new node;
      |                                  ~~~~~^
necklace.cpp:39:24: warning: array subscript has type 'char' [-Wchar-subscripts]
   39 |         r = r->nx[S[f+i]];
      |                   ~~~~~^
necklace.cpp: In function 'short int fi(node*)':
necklace.cpp:47:24: warning: array subscript has type 'char' [-Wchar-subscripts]
   47 |         if(!r->nx[S[f+i]]) return -1;
      |                   ~~~~~^
necklace.cpp:48:24: warning: array subscript has type 'char' [-Wchar-subscripts]
   48 |         r = r->nx[S[f+i]];
      |                   ~~~~~^
# Verdict Execution time Memory Grader output
1 Correct 5 ms 3928 KB Output is correct
2 Correct 5 ms 3164 KB Output is correct
3 Correct 4 ms 3164 KB Output is correct
4 Correct 12 ms 11356 KB Output is correct
5 Correct 21 ms 32848 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 3928 KB Output is correct
2 Correct 5 ms 3164 KB Output is correct
3 Correct 4 ms 3164 KB Output is correct
4 Correct 12 ms 11356 KB Output is correct
5 Correct 21 ms 32848 KB Output is correct
6 Correct 249 ms 24180 KB Output is correct
7 Runtime error 284 ms 225600 KB Execution killed with signal 11
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 5 ms 3928 KB Output is correct
2 Correct 5 ms 3164 KB Output is correct
3 Correct 4 ms 3164 KB Output is correct
4 Correct 12 ms 11356 KB Output is correct
5 Correct 21 ms 32848 KB Output is correct
6 Correct 249 ms 24180 KB Output is correct
7 Runtime error 284 ms 225600 KB Execution killed with signal 11
8 Halted 0 ms 0 KB -