yaoxi-std 的博客

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

0%

P4081 [USACO17DEC] Standing Out from the Herd P

P4081 [USACO17DEC] Standing Out from the Herd P

题面

题目链接

解法

首先构建后缀数组。仍然套路地枚举大串的位置$sa[i]$。首先考虑的是不能在别的小串中出现,记录其前后第一个在不同串中出现的后缀位置,分别为$pre_i$和$nxt_i$,则以$sa[i]$开头的后缀对答案贡献的最小长度为$\max(lcs(pre_i, i), lcs(i, nxt_i))$;接下来考虑只在当前小串中出现一次是,则对答案贡献的最小长度为$\max(lcs(i,i+1))$。两者取$\max$即可。总的时间复杂度为$O(n \log n)$。

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
/**
* @file: P4081.cpp
* @author: yaoxi-std
* @url: https://www.luogu.com.cn/problem/P4081
*/
// #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 = 2e5 + 10;
const int INF = 0x3f3f3f3f3f3f3f3f;
struct SuffixArray {
int n, sa[MAXN], rk[MAXN], tp[MAXN], ht[MAXN], he[MAXN];
void radix_sort(int m) {
static int buc[MAXN];
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++;
ht[i] = k;
}
for (int i = 1; i <= n; ++i)
he[i] = ht[sa[i]];
}
} sa;
int n, m, id[MAXN], ed[MAXN], len[MAXN], ans[MAXN];
char buf[MAXN], str[MAXN];
signed main() {
read(n), m = 1;
for (int i = 1; i <= n; ++i) {
scanf("%s", buf);
len[i] = strlen(buf);
copy(buf, buf + len[i], str + m);
str[m + len[i]] = '$';
fill(id + m, id + m + len[i], i);
ed[i] = m + len[i];
m += len[i] + 1;
}
sa.init(m, str);
int pre = 0, mn = INF;
for (int i = 1; i <= m; ++i) {
if (id[sa.sa[i]] != id[sa.sa[i - 1]]) {
int p = id[sa.sa[i - 1]];
pre = i - 1, mn = ed[p] - sa.sa[i - 1];
}
mn = min(mn, sa.he[i]);
int p = id[sa.sa[i]];
int cur = max(0ll, ed[p] - sa.sa[i] - max(mn, sa.he[i + 1]));
ans[p] += cur;
}
for (int i = 1; i <= n; ++i)
write(ans[i]), putchar('\n');
return 0;
}