yaoxi-std 的博客

$\text{开}\mathop{\text{卷}}\limits^{ju\check{a}n}\text{有益}$

0%

P3900 [湖南集训] 图森

P3900 [湖南集训] 图森

题面

题目链接

解法

代码极其恶心,所以简单写下思路。

基本思路是$dp$和建图。首先将所有串正向和反向串起来,这样$dp[i]$就表示当前回文串的其中一边拓展到了端点,另一边拓展到$i$的位置。往后转移只要枚举在其中一边加上哪个小串,用后缀数组$O(1)$找到能拓展几位。将这个$dp$的转移建图,如果不是DAG则显然有无限长度,输出Infinity。否则在上面做DAG最长路即可。时间复杂度$O(n^2L)$。

接下来是特殊处理。首先是初始状态,需要找出每个小串的前缀回文串和后缀回文串。这个用SA似乎可以暴力做。其次是图的边集$E$的大小,应当于时间复杂度同阶,显然不可能存下来,所以需要在每次做DAG时求一边边集,这样就保证空间复杂度不会太高。

除此之外,你还需要计算每个小串中包含的最长回文串。。。不然只有$90pts$(别问我怎么知道的)。所以再写个manacher吧。

下面放出我shit一样的代码

AC代码

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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/**
* @file: P3900.cpp
* @author: yaoxi-std
* @url: https://www.luogu.com.cn/problem/P3900
*/
// #pragma GCC optimize ("O2")
// #pragma GCC optimize ("Ofast", "inline", "-ffast-math")
// #pragma GCC target ("avx,sse2,sse3,sse4,mmx")
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define resetIO(x) \
freopen(#x ".in", "r", stdin), freopen(#x ".out", "w", stdout)
#define debug(fmt, ...) \
fprintf(stderr, "[%s:%d] " fmt "\n", __FILE__, __LINE__, ##__VA_ARGS__)
template <class _Tp>
inline _Tp& read(_Tp& x) {
bool sign = false;
char ch = getchar();
long double tmp = 1;
for (; !isdigit(ch); ch = getchar())
sign |= (ch == '-');
for (x = 0; isdigit(ch); ch = getchar())
x = x * 10 + (ch ^ 48);
if (ch == '.')
for (ch = getchar(); isdigit(ch); ch = getchar())
tmp /= 10.0, x += tmp * (ch ^ 48);
return sign ? (x = -x) : x;
}
template <class _Tp>
inline void write(_Tp x) {
if (x < 0)
putchar('-'), x = -x;
if (x > 9)
write(x / 10);
putchar((x % 10) ^ 48);
}
const int MAXN = 120;
const int MAXM = 1e6 + 10;
const int LOGM = 20;
const int INF = 0x3f3f3f3f3f3f3f3f;
struct SuffixArray {
int n, sa[MAXM], rk[MAXM], tp[MAXM], he[MAXM];
void radix_sort(int m) {
static int buc[MAXM];
for (int i = 0; i <= m; ++i)
buc[i] = 0;
for (int i = 1; i <= n; ++i)
buc[rk[i]]++;
for (int i = 1; i <= m; ++i)
buc[i] += buc[i - 1];
for (int i = n; i >= 1; --i)
sa[buc[rk[tp[i]]]--] = tp[i];
}
void init(int n, char* s) {
this->n = n;
int m = 200;
for (int i = 1; i <= n; ++i)
rk[i] = s[i] + 1, tp[i] = i;
radix_sort(m);
for (int w = 1, p = 0; p < n; m = p, w <<= 1) {
p = 0;
for (int i = 1; i <= w; ++i)
tp[++p] = n - w + i;
for (int i = 1; i <= n; ++i)
if (sa[i] > w)
tp[++p] = sa[i] - w;
radix_sort(m);
copy(rk + 1, rk + n + 1, tp + 1);
rk[sa[1]] = p = 1;
for (int i = 2; i <= n; ++i) {
if (tp[sa[i - 1]] == tp[sa[i]] && tp[sa[i - 1] + w] == tp[sa[i] + w])
rk[sa[i]] = p;
else
rk[sa[i]] = ++p;
}
}
for (int i = 1, k = 0; i <= n; ++i) {
if (k)
k--;
while (s[i + k] == s[sa[rk[i] - 1] + k])
k++;
he[rk[i]] = k;
}
}
} sa;
int n, m, tot, cnt, ans;
int fr[MAXN], bk[MAXN], ln[MAXN];
int lg2[MAXM], st[MAXM][LOGM];
int id[MAXM], dis[MAXM], deg[MAXM];
char buf[MAXM], str[MAXM];
vector<int> palfr[MAXN], palbk[MAXN];
int getlcp(int x, int y) {
int l = sa.rk[x], r = sa.rk[y];
if (l > r)
swap(l, r);
if (l == r)
return INF;
int k = lg2[r - l];
return min(st[l + 1][k], st[r - (1 << k) + 1][k]);
}
void findedges(int i, function<void(int, int, int)> addedge) {
if (i == 0) {
for (int j = 1; j <= n; ++j) {
for (auto l : palfr[j])
addedge(i, fr[j] + l, l);
for (auto l : palbk[j])
addedge(i, bk[j] + l, l);
}
return;
}
if (i > m || str[i] == '$')
return;
int t = upper_bound(fr + 1, fr + n + 1, i) - fr - 1;
if (i < bk[t]) {
int len = ln[t] - (i - fr[t]);
for (int j = 1; j <= n; ++j) {
int lcp = min({len, ln[j], getlcp(i, bk[j])});
if (lcp == len && lcp == ln[j]) {
addedge(i, 0, lcp * 2);
} else if (lcp == len) {
addedge(i, bk[j] + lcp, lcp * 2);
} else if (lcp == ln[j]) {
addedge(i, i + lcp, lcp * 2);
} else {
addedge(i, m + 1, lcp * 2);
}
}
} else {
int len = ln[t] - (i - bk[t]);
for (int j = 1; j <= n; ++j) {
int lcp = min({len, ln[j], getlcp(i, fr[j])});
if (lcp == len && lcp == ln[j]) {
addedge(i, 0, lcp * 2);
} else if (lcp == len) {
addedge(i, fr[j] + lcp, lcp * 2);
} else if (lcp == ln[j]) {
addedge(i, i + lcp, lcp * 2);
} else {
addedge(i, m + 1, lcp * 2);
}
}
}
}
int manacher(char* s, int tlen) {
static int d[MAXM];
static char t[MAXM];
int len = 0;
for (int i = 1; i <= tlen; ++i)
t[++len] = '#', t[++len] = s[i];
t[++len] = '#', t[++len] = '$';
int mx = 0, id = 0, ans = 0;
for (int i = 1; i <= len; ++i) {
d[i] = mx > i ? min(d[id * 2 - i], mx - i) : 1;
while (t[i + d[i]] == t[i - d[i]])
++d[i];
if (i + d[i] > mx)
mx = i + d[i], id = i;
ans = max(ans, d[i] - 1);
}
return ans;
}
signed main() {
read(n), m = 1;
for (int i = 1; i <= n; ++i) {
scanf("%s", buf);
int t = ln[i] = strlen(buf);
copy(buf, buf + t, str + m);
fr[i] = m, str[m + t] = '$', m += t + 1;
reverse(buf, buf + t);
copy(buf, buf + t, str + m);
bk[i] = m, str[m + t] = '$', m += t + 1;
}
str[m] = '$';
sa.init(m, str);
for (int i = 2; i <= m; ++i)
lg2[i] = (i & (i - 1)) ? lg2[i - 1] : lg2[i - 1] + 1;
for (int i = 1; i <= m; ++i)
st[i][0] = sa.he[i];
for (int j = 1; j < LOGM; ++j) {
for (int i = 1; i + (1 << j) - 1 <= m; ++i) {
st[i][j] = min(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]);
}
}
for (int i = 1; i <= n; ++i) {
palfr[i].push_back(0);
palfr[i].push_back(1);
for (int j = 1; j + j <= ln[i]; ++j) {
int p1 = fr[i] + j, p2 = bk[i] + (ln[i] - j);
if (getlcp(p1, p2) >= j)
palfr[i].push_back(j + j);
if (j + j < ln[i]) {
p1 = fr[i] + j + 1, p2 = bk[i] + (ln[i] - j);
if (getlcp(p1, p2) >= j)
palfr[i].push_back(j + j + 1);
}
}
palbk[i].push_back(0);
palbk[i].push_back(1);
for (int j = 1; j + j <= ln[i]; ++j) {
int p1 = bk[i] + j, p2 = fr[i] + (ln[i] - j);
if (getlcp(p1, p2) >= j)
palbk[i].push_back(j + j);
if (j + j < ln[i]) {
p1 = bk[i] + j + 1, p2 = fr[i] + (ln[i] - j);
if (getlcp(p1, p2) >= j)
palbk[i].push_back(j + j + 1);
}
}
}
for (int i = 0; i <= m; ++i) {
findedges(i, [&](int u, int v, int w) -> void {
++deg[v];
});
}
queue<int> que;
for (int i = 0; i <= m + 1; ++i)
if (!deg[i])
que.push(i);
while (!que.empty()) {
int i = que.front();
id[++cnt] = i, que.pop();
findedges(i, [&](int u, int v, int w) -> void {
if (--deg[v] == 0)
que.push(v);
});
}
for (int i = 0; i <= m + 1; ++i)
if (deg[i])
return puts("Infinity"), 0;
memset(dis, -0x3f, sizeof(dis));
dis[0] = 0;
for (int i = 1; i <= cnt; ++i) {
int x = id[i];
findedges(x, [&](int u, int v, int w) -> void {
dis[v] = max(dis[v], dis[u] + w);
});
}
ans = dis[m + 1];
for (int i = 1; i <= n; ++i)
ans = max(ans, manacher(str + fr[i] - 1, ln[i]));
write(ans), putchar('\n');
return 0;
}