Blog of RuSun

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

CF432D Prefixes and Suffixes

LuoGu: CF432D Prefixes and Suffixes

CF: D. Prefixes and Suffixes

求出 Z函数,合法当且仅当 $z _ i = n - i$ 。

查看代码
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
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long L;
const int N = 1e5 + 10;
char s[N];
int n, f[N], g[N];
void init ()
{
for (int i = 1, t = 0; i < n; ++i)
{
if (t + f[t] > i) f[i] = min(f[i - t], t + f[t] - i);
while (i + f[i] < n && s[f[i]] == s[i + f[i]]) ++f[i];
if (i + f[i] > t + f[t]) t = i;
}
f[0] = n;
}
int main ()
{
scanf("%s", s);
n = strlen(s);
init();
int res = 0;
for (int i = 0; i < n; ++i)
{
++g[f[i]];
if (f[i] == n - i) ++res;
}
for (int i = n - 1; i; --i) g[i] += g[i + 1];
printf("%d\n", res);
for (int i = n - 1; ~i; --i)
if (f[i] == n - i) printf("%d %d\n", f[i], g[f[i]]);
return 0;
}