HDU 5769 Substring(后缀数组)

题意:

$给定N\le 10^5的字符串,求包含一个特定字母X的不同子串的个数$

分析:

$考虑不带特定字母的版本,对于每个后缀sa[i]的贡献是n-sa[i]-height[i]$
$由于必须包含X,所以对于每个sa[i],记录一下之后最近的X的位置nxt[sa[i]]$
$那么贡献应该是n-nxt[sa[i]],两者取min就好了$
$即n-max(nxt[sa[i]], sa[i]+height[i])为每个sa[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
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
//
// Created by TaoSama on 2016-08-01
// Copyright (c) 2016 TaoSama. All rights reserved.
//
#pragma comment(linker, "/STACK:102400000,102400000")
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <vector>
using namespace std;
#define pr(x) cout << #x << " = " << x << " "
#define prln(x) cout << #x << " = " << x << endl
const int N = 1e5 + 10, INF = 0x3f3f3f3f, MOD = 1e9 + 7;
int n;
char str[N], x[2];
int s[N], c[N], t[2][N];
int sa[N], rk[N], height[N];
// sa[0] is empty suffix, 1 ~ n
// height[i]:= lcp of sa[i-1] and sa[i]
void build(int n, int m) {
int i, k, p, *x = t[0], *y = t[1];
memset(c, 0, m << 2);
for(i = 0; i < n; ++i) ++c[x[i] = s[i]];
for(i = 1; i < m; ++i) c[i] += c[i - 1];
for(i = n - 1; i >= 0; --i) sa[--c[x[i]]] = i;
for(k = 1; k <= n; k <<= 1) {
p = 0;
for(i = n - k; i < n; ++i) y[p++] = i;
for(i = 0; i < n; ++i) if(sa[i] >= k) y[p++] = sa[i] - k;
memset(c, 0, m << 2);
for(i = 0; i < n; ++i) ++c[x[y[i]]];
for(i = 1; i < m; ++i) c[i] += c[i - 1];
for(i = n - 1; i >= 0; --i) sa[--c[x[y[i]]]] = y[i];
swap(x, y); p = 1; x[sa[0]] = 0;
for(i = 1; i < n; ++i)
x[sa[i]] = (y[sa[i - 1]] == y[sa[i]] &&
y[sa[i - 1] + k] == y[sa[i] + k]) ? p - 1 : p++;
if(p >= n) break; m = p;
}
for(i = 0; i < n; ++i) rk[sa[i]] = i;
for(i = 0, k = 0; i < n; ++i) {
if(k) k--;
if(!rk[i]) continue;
int j = sa[rk[i] - 1];
while(s[i + k] == s[j + k]) k++;
height[rk[i]] = k;
}
}
void see(int n) {
for(int i = 0; i < n; ++i) printf("%d: %s\n", i, str + sa[i]);
printf("sa: ");
for(int i = 0; i < n; ++i) printf("%d ", sa[i]); puts("");
printf("rk: ");
for(int i = 0; i < n; ++i) printf("%d ", rk[i]); puts("");
printf("ht: ");
for(int i = 0; i < n; ++i) printf("%d ", height[i]); puts("");
}
int nxt[N];
int main() {
#ifdef LOCAL
freopen("C:\\Users\\TaoSama\\Desktop\\in.txt", "r", stdin);
// freopen("C:\\Users\\TaoSama\\Desktop\\out.txt","w",stdout);
#endif
ios_base::sync_with_stdio(0);
int t; scanf("%d", &t);
while(t--) {
scanf("%s%s", x, str);
n = strlen(str);
for(int i = 0; i < n; ++i) s[i] = str[i] - 'a' + 1;
s[n] = 0;
build(n + 1, 30);
// see(n + 1);
nxt[n] = n;
for(int i = n - 1; ~i; --i) {
nxt[i] = nxt[i + 1];
if(str[i] == *x) nxt[i] = i;
}
long long ans = 0;
for(int i = 1; i <= n; ++i) {
ans += n - max(nxt[sa[i]], sa[i] + height[i]);
}
static int kase = 0;
printf("Case #%d: %I64d\n", ++kase, ans);
}
return 0;
}


1. 除非注明,本博文即为原创,转载请注明链接地址
2. 本博文只代表博主当时的观点或结论,请不要恶意攻击
3. 如果本文帮到了您,不妨点一下 下面分享到 按钮,让更多的人看到