Submission #138182

#TimeUsernameProblemLanguageResultExecution timeMemory
138182popovicirobertNecklace (Subtask 1-3) (BOI19_necklace1)C++14
85 / 85
481 ms71160 KiB
#include <bits/stdc++.h>
#pragma GCC optimize ("Ofast")
#define lsb(x) (x & (-x))
#define ll long long
#define ull unsigned long long
// 217
// 44


/*const int MOD = (int) 1e9 + 7;

inline int lgput(int a, int b) {
    int ans = 1;
    while(b > 0) {
        if(b & 1) ans = (1LL * ans * a) % MOD;
        b >>= 1;
        a = (1LL * a * a) % MOD;
    }
    return ans;
}

inline void mod(int &x) {
    if(x >= MOD)
        x -= MOD;
}

inline void add(int &x, int y) {
    x += y;
    mod(x);
}

inline void sub(int &x, int y) {
    x += MOD - y;
    mod(x);
}

inline void mul(int &x, int y) {
    x = (1LL * x * y) % MOD;
}*/

/*int fact[], invfact[];

inline void prec(int n) {
    fact[0] = 1;
    for(int i = 1; i <= n; i++) {
        fact[i] = (1LL * fact[i - 1] * i) % MOD;
    }
    invfact[n] = lgput(fact[n], MOD - 2);
    for(int i = n - 1; i >= 0; i--) {
        invfact[i] = (1LL * invfact[i + 1] * (i + 1)) % MOD;
    }
}

inline int comb(int n, int k) {
    if(n < k) return 0;
    return (1LL * fact[n] * (1LL * invfact[k] * invfact[n - k] % MOD)) % MOD;
}*/

using namespace std;

const int MAXN = 3005;

char a[MAXN + 1], b[MAXN + 1];
int dpa[MAXN + 1][MAXN + 1], dpb[MAXN + 1][MAXN + 1];
int n, m;

char str[2 * MAXN + 1];
int pi[2 * MAXN + 1];

inline void get(char *a, char *b, int n, int m, int dp[MAXN + 1][MAXN + 1]) {
    int i, j;
    for(i = 1; i <= n; i++) {

        int sz = 0;
        for(j = i; j <= n; j++) {
            str[++sz] = a[j];
        }
        str[++sz] = '0';
        for(j = 1; j <= m; j++) {
            str[++sz] = b[j];
        }

        int k = 0;
        for(j = 2; j <= sz; j++) {
            while(k > 0 && str[k + 1] != str[j]) {
                k = pi[k];
            }
            if(str[k + 1] == str[j]) {
                k++;
            }
            pi[j] = k;
            if(j >= n - i + 3) {
                dp[i][j - (n - i) - 2] = pi[j];
            }
        }
    }
}

struct Data {
    int val;
    int posa, posb;
    bool operator <(const Data &other) const {
        return val < other.val;
    }
};

inline Data solve() {
    get(a, b, n, m, dpa);
    get(b, a, m, n, dpb);

    Data ans = {0, 0, 0};
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= m; j++) {
            int cur = dpa[i][j - 1] + dpb[j][i - 1];
            if(cur > ans.val) {
                ans.val = cur;
                ans.posa = i - dpb[j][i - 1];
                ans.posb = j - dpa[i][j - 1];
            }
        }
    }

    return ans;
}

int main() {
    //ifstream cin("A.in");
    //ofstream cout("A.out");
    //int ;
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);

    cin >> a + 1 >> b + 1;
    n = strlen(a + 1), m = strlen(b + 1);

    Data x = solve();
    reverse(a + 1, a + n + 1);
    Data y = solve();
    y.posa = n - y.posa + 1 - y.val + 1;

    if(x < y) {
        swap(x, y);
    }

    cout << x.val << "\n" << x.posa - 1 << " " << x.posb - 1;

    return 0;
}

Compilation message (stderr)

necklace.cpp: In function 'int main()':
necklace.cpp:133:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
     cin >> a + 1 >> b + 1;
            ~~^~~
necklace.cpp:133:23: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
     cin >> a + 1 >> b + 1;
                     ~~^~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...