P7114 字符串匹配

求 $S = {(AB)}^iC$的方案数,其中 $F(A) \le F(C)$ 表示字符串 $S$ 中出现奇数次的字符的数量

枚举$(AB)^i$,对于$C$寻找符合条件的$AB$.

复杂度为$O(n(26+\log n))$

代码
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


#include <bits/stdc++.h>
using namespace std;

const int N = 2e6 + 10;

#define ll long long
void getnext(char *t, int *nxt)
{
nxt[0] = -1;
int m = strlen(t + 1);
for (int i = 1, j; i <= m; i++)
{
for (j = nxt[i - 1]; j >= 0 && t[j + 1] != t[i]; j = nxt[j])
;
nxt[i] = j + 1;
}
}

char t[N], s[N];
int nxt[N];
int pre[N], suf[N], cnt[27], len[N];
int to[27];
int main()
{
int T;
scanf("%d", &T);
while (T--)
{
scanf("%s", s + 1);
int n = strlen(s + 1);

for (int i = 1; i <= n; i++)
nxt[i] = 0, len[i] = 1;
getnext(s, nxt);
memset(cnt, 0, sizeof(cnt));
memset(to, 0, sizeof(to));
int res = 0;
for (int i = 1; i <= n; i++)
{
int o = s[i] - 'a';
res -= cnt[o] & 1;
cnt[o]++;
res += cnt[o] & 1;
pre[i] = res;
}

memset(cnt, 0, sizeof(cnt));
res = 0;
for (int i = n; i >= 1; i--)
{
int o = s[i] - 'a';
res -= cnt[o] & 1;
cnt[o]++;
res += cnt[o] & 1;
suf[i] = res;
}

ll ans = 0;
for (int i = 2; i < n; i++)
{

for (int j = pre[i - 1]; j <= 26; j++)
to[j]++;
ans += to[suf[i + 1]];
for (int j = 2 * i; j < n; j += i)
{
if (((i % (j - nxt[j])) == 0) && ((j / (j - nxt[j])) > 1))
ans += to[suf[j + 1]];
else
break;
}
}
printf("%lld\n", ans);
}
}