1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
| #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int N = 2e5 + 10; struct Node { int fa, len; int nxt[26]; } tr[N]; char str[N]; int n, m, ans[N], last = 1, cnt = 1; void insert(int c) { int p = last, np = ++cnt; tr[np].len = tr[p].len + 1; for (; p && !tr[p].nxt[c]; p = tr[p].fa) tr[p].nxt[c] = np; last = cnt; if (!p) { tr[np].fa = 1; return; } int q = tr[p].nxt[c]; if (tr[q].len == tr[p].len + 1) { tr[np].fa = q; return; } int nq = ++cnt; tr[nq] = tr[q], tr[nq].len = tr[p].len + 1; tr[q].fa = tr[np].fa = nq; for (; p && tr[p].nxt[c] == q; p = tr[p].fa) tr[p].nxt[c] = nq; } int f[N]; int idx, hd[N], nxt[N], edg[N]; void add(int a, int b) { nxt[++idx] = hd[a]; hd[a] = idx; edg[idx] = b; } void dfs(int x) { for (int i = hd[x]; ~i; i = nxt[i]) { dfs(edg[i]); f[x] = max(f[x], f[edg[i]]); } } int main() { scanf("%d%s", &m, str + 1); m--; n = strlen(str + 1); for (int i = 1; i <= n; i++) insert(str[i] - 'a'); for (int i = 1; i <= cnt; i++) ans[i] = tr[i].len; for (int i = 1; i <= cnt; i++) hd[i] = -1; for (int i = 2; i <= cnt; i++) add(tr[i].fa, i); while (m--) { scanf("%s", str + 1); n = strlen(str + 1); for (int i = 1; i <= cnt; i++) f[i] = 0; int p = 1, t = 0; for (int i = 1; i <= n; i++) { int c = str[i] - 'a'; while (p > 1 && !tr[p].nxt[c]) { p = tr[p].fa; t = tr[p].len; } if (tr[p].nxt[c]) { p = tr[p].nxt[c]; t++; } f[p] = max(f[p], t); } dfs(1); for (int i = 1; i <= cnt; i++) ans[i] = min(ans[i], f[i]); } int res = 0; for (int i = 1; i <= cnt; i++) res = max(res, ans[i]); printf("%d", res); return 0; }
|