给出nn个由小写字母构成的字符串,求它们最长的公共子串的长度

n5,S10000n\le 5, \sum|S|\le 10000

BZOJ2946

Solution

做法见此

稍微讲下,就是建出第一个串的SAM,然后把后面的串放上面匹配

当然也可以直接上广义SAM,但是我不会

Code

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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include <bits/stdc++.h>

#define x first
#define y second
#define y1 Y1
#define y2 Y2
#define mp make_pair
#define pb push_back

using namespace std;

typedef long long LL;
typedef pair <int, int> pii;

template <typename T> inline int Chkmax (T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> inline int Chkmin (T &a, T b) { return a > b ? a = b, 1 : 0; }
template <typename T> inline T read ()
{
T sum = 0, fl = 1; char ch = getchar();
for (; !isdigit(ch); ch = getchar()) if (ch == '-') fl = -1;
for (; isdigit(ch); ch = getchar()) sum = (sum << 3) + (sum << 1) + ch - '0';
return sum * fl;
}

inline void proc_status ()
{
ifstream t ("/proc/self/status");
cerr << string (istreambuf_iterator <char> (t), istreambuf_iterator <char> ()) << endl;
}

const int MAXN = 2000 + 100;

int N, M;
char S[MAXN];

namespace SAM
{
const int MAX_NODE = MAXN << 1;
int node_cnt = 1, last = 1;

struct info
{
int ch[27], fa, maxlen, max;
} node[MAX_NODE];

inline int new_node (int pre)
{
node[++node_cnt].maxlen = node[pre].maxlen + 1;
return node_cnt;
}

inline void extend (int c)
{
int now = new_node (last), pre = last; last = now;

for (; pre && !node[pre].ch[c]; pre = node[pre].fa) node[pre].ch[c] = now;

if (!pre) node[now].fa = 1;
else
{
int x = node[pre].ch[c];
if (node[x].maxlen == node[pre].maxlen + 1) node[now].fa = x;
else
{
int y = ++node_cnt;
node[y] = node[x]; node[y].maxlen = node[pre].maxlen + 1;
node[x].fa = node[now].fa = y;

for (; node[pre].ch[c] == x; pre = node[pre].fa) node[pre].ch[c] = y;
}
}
}

vector <int> G[MAX_NODE];

int Ans[MAX_NODE];

inline void build (char *s, int n)
{
for (int i = 1; i <= n; ++i) extend (s[i] - 'a');
for (int i = 1; i <= node_cnt; ++i) G[node[i].fa].pb(i);
for (int i = 1; i <= node_cnt; ++i) Ans[i] = node[i].maxlen;
}

inline void dfs (int x)
{
for (int i = 0; i < G[x].size(); ++i)
{
int y = G[x][i];
dfs (y);
Chkmax (node[x].max, node[y].max);
}
}

inline int get_ans ()
{
int ans = 0;
for (int i = 1; i <= node_cnt; ++i) Chkmax (ans, Ans[i]);
return ans;
}

inline void query (char *s, int n)
{
for (int i = 1; i <= node_cnt; ++i) node[i].max = 0;

int now = 1, len = 0;
for (int i = 1; i <= n; ++i)
{
int c = s[i] - 'a';

while (1)
{
if (node[now].ch[c])
{
now = node[now].ch[c], ++len, Chkmax(node[now].max, len);
break;
}
else
{
if (!len) break;
if ((--len) == node[node[now].fa].maxlen) now = node[now].fa;
}
}
}

dfs (1);
for (int i = 1; i <= node_cnt; ++i) Chkmin (Ans[i], node[i].max);
}

}

inline void Solve ()
{
scanf("%s", S + 1), M = strlen(S + 1);
SAM :: build (S, M);

for (int i = 2; i <= N; ++i)
{
scanf("%s", S + 1), M = strlen(S + 1);
SAM :: query (S, M);
}

printf("%d\n", SAM :: get_ans ());
}

inline void Input ()
{
N = read<int>();
}

int main()
{

#ifndef ONLINE_JUDGE
freopen("A.in", "r", stdin);
freopen("A.out", "w", stdout);
#endif

Input ();
Solve ();

return 0;
}