【BZOJ4698】【SDOI2008】Sandy的卡片(SAM)

Description

click me


Solution

练习SAM板子,顺便刷经验233

这道题和SPOJ LCS2差不多。
写的时候有个错误WA了好久:min[u]一定要赋成len[u]!!!不然用儿子更新父亲的时候会超过len[u]


Code

/************************************************
 * Au: Hany01
 * Prob: [BZOJ4698][SDOI2008] Sandy的卡片
 * Date: May 29th, 2018
 * Email: [email protected]
************************************************/

#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/hash_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef long long LL;
typedef pair<int, int> PII;
#define File(a) freopen(a".in", "r", stdin), freopen(a".out", "w", stdout)
#define rep(i, j) for (register int i = 0, i##_end_ = (j); i < i##_end_; ++ i)
#define For(i, j, k) for (register int i = (j), i##_end_ = (k); i <= i##_end_; ++ i)
#define Fordown(i, j, k) for (register int i = (j), i##_end_ = (k); i >= i##_end_; -- i)
#define Set(a, b) memset(a, b, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define x first
#define y second
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define ALL(a) (a).begin(), (a).end()
#define SZ(a) ((int)(a).size())
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define Mod (1000000007)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define y1 wozenmezhemecaia

template <typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }

inline int read()
{
    register int _, __; register char c_;
    for (_ = 0, __ = 1, c_ = getchar(); c_ < '0' || c_ > '9'; c_ = getchar()) if (c_ == '-') __ = -1;
    for ( ; c_ >= '0' && c_ <= '9'; c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
    return _ * __;
}

const int maxn = 1005;

int n, fa[maxn << 1], len[maxn << 1], s[maxn], T, mn[maxn << 1], mx[maxn << 1], tot = 1, las = 1, bkt[maxn], per[maxn << 1];
cc_hash_table<int, int> ch[maxn << 1];

inline void extend(int c)
{
    int np = ++ tot, p = las;
    las = np, len[np] = len[p] + 1;
    while (p && !ch[p][c]) ch[p][c] = np, p = fa[p];
    if (!p) fa[np] = 1;
    else {
        int q = ch[p][c];
        if (len[q] == len[p] + 1) fa[np] = q;
        else {
            int nq = ++ tot;
            ch[nq] = ch[q], fa[nq] = fa[q], len[nq] = len[p] + 1, fa[q] = fa[np] = nq;
            while (p && ch[p][c] == q) ch[p][c] = nq, p = fa[p];
        }
    }
}

int main()
{
#ifdef hany01
    File("bzoj4698");
#endif

    T = read(), n = read();
    For(i, 1, n) s[i] = read();
    For(i, 1, n - 1) extend(s[i + 1] - s[i]);

    For(i, 1, tot) ++ bkt[len[i]];
    For(i, 1, n) bkt[i] += bkt[i - 1];
    For(i, 1, tot) per[bkt[len[i]] --] = i;

    For(i, 1, tot) mn[i] = len[i];
    while (-- T)
    {
        int L = 0, u = 1, c;
        n = read(), s[1] = read(), Set(mx, 0);
        For(i, 2, n) {
            s[i] = read(), c = s[i] - s[i - 1];
            if (ch[u][c]) u = ch[u][c], ++ L;
            else {
                while (u && !ch[u][c]) u = fa[u];
                if (!u) u = 1, L = 0;
                else L = len[u] + 1, u = ch[u][c];
            }
            chkmax(mx[u], L);
        }
        Fordown(i, tot, 1) chkmax(mx[fa[per[i]]], mx[per[i]]);
        For(i, 1, tot) chkmin(mn[i], mx[i]);
    }

    static int Max = 0;
    For(i, 1, tot) chkmax(Max, mn[i]);
    printf("%d\n", Max + 1);

    return 0;
}
//桂魄初生秋露微,轻罗已薄未更衣。
//    -- 王维《秋夜曲》

猜你喜欢

转载自blog.csdn.net/hhaannyyii/article/details/80496192