Blog of RuSun

\begin {array}{c} \mathfrak {One Problem Is Difficult} \\\\ \mathfrak {Because You Don't Know} \\\\ \mathfrak {Why It Is Diffucult} \end {array}

CF1200E Compress Words

LuoGu: CF1200E Compress Words

CF: E. Compress Words

需要一个串的前缀和一个串的后缀匹配,用 KMP 不好思考。直接用字符串哈希。

查看代码
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
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
template <class Type>
void read (Type &x)
{
char c;
bool flag = false;
while ((c = getchar()) < '0' || c > '9')
c == '-' && (flag = true);
x = c - '0';
while ((c = getchar()) >= '0' && c <= '9')
x = (x << 1) + (x << 3) + c - '0';
flag && (x = ~x + 1);
}
template <class Type, class ...rest>
void read (Type &x, rest &...y) { read(x), read(y...); }
template <class Type>
void write (Type x)
{
x < 0 && (putchar('-'), x = ~x + 1);
x > 9 && (write(x / 10), 0);
putchar('0' + x % 10);
}
typedef long long LL;
const int N = 1e6 + 10;
const int base = 131, ibase = 922042494, mod = 998244353;
int s[N], t[N];
char c[N], ans[N];
int n, m, len, pbase[N];
int main ()
{
pbase[0] = 1;
for (int i = 1; i < N; ++i)
pbase[i] = (LL)pbase[i - 1] * base % mod;
read(n);
for (int i = 1; i <= n; ++i)
{
scanf("%s", c + 1);
m = strlen(c + 1);
for (int k = 1; k <= m; ++k)
t[k] = ((LL)c[k] * pbase[k] + t[k - 1]) % mod;
for (int k = min(m, len); ~k; --k)
if ((s[len] - s[len - k] + mod) % mod == (LL)t[k] * pbase[len - k] % mod)
{
for (int j = k + 1; j <= m; ++j)
{
++len;
ans[len] = c[j];
s[len] = ((LL)c[j] * pbase[len] + s[len - 1]) % mod;
}
break;
}
}
for (int i = 1; i <= len; ++i) putchar(ans[i]);
return 0;
}