0%

[HEOI2015]最短不公共子串

题面描述

求两个字符串最短不公共子序列和子串

题解

我们需要两个自动机,一个可以接受所有子串,另一个可以接受所有子序列,前者我们可以使用 \(SAM\),后者我们要构造一个叫做序列自动机的东西

序列自动机的构造不难理解,记录每个字符上一个出现位置,连转移边即可,构造复杂度为 \(O(n|S|)\),感性理解下就是每一次加入的复杂度等价为从这一次加入到上一加入之间有多少字符,单看每个字符这个整体求和后是 \(O(n)\),字符集大小为 \(|S|\),所以复杂度为 \(O(n|S|)\)

1
2
3
4
5
6
7
8
9
10
11
12
13
struct Sqam
{
int root, tot, last[26], pre[N], ch[N][26];
Sqam() { root = tot = 1; for(int i = 0; i < 26; ++i) last[i] = 1; }
void insert(int c)
{
int cur = ++tot; pre[cur] = last[c];
for(int i = 0; i < 26; ++i)
for(int k = last[i]; k&&!ch[k][c]; k = pre[k])
ch[k][c] = cur;
last[c] = cur;
}
}

那么这题就用 \(BFS\) 暴力匹配即可

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
116
117
118
119
120
121
122
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cctype>
#include <tuple>
#include <queue>
using namespace std;
const int N = 4e3+5;
struct Sam
{
int tot = 1, last = 1, ch[N][26], len[N], fa[N];
void insert(int c)
{
int cur = ++tot, pre = last; last = tot;
len[cur] = len[pre]+1;
while(pre&&!ch[pre][c]) ch[pre][c] = cur, pre = fa[pre];
if(!pre) return void(fa[cur] = 1);
int x = ch[pre][c];
if(len[x] == len[pre]+1) return void(fa[cur] = x);
int y = ++tot; fa[y] = fa[x]; fa[cur] = fa[x] = y; len[y] = len[pre]+1;
memcpy(ch[y], ch[x], sizeof(ch[x]));
while(pre&&ch[pre][c] == x) ch[pre][c] = y, pre = fa[pre];
}
}sama, samb;
struct Sqam
{
int root, tot, last[26], pre[N], ch[N][26];
Sqam() { root = tot = 1; for(int i = 0; i < 26; ++i) last[i] = 1; }
void insert(int c)
{
int cur = ++tot; pre[cur] = last[c];
for(int i = 0; i < 26; ++i)
for(int k = last[i]; k&&!ch[k][c]; k = pre[k])
ch[k][c] = cur;
last[c] = cur;
}
}sqama, sqamb;
char s[N];
int d[N][N];
int bfs1()
{
queue<pair<int, int> > q;
memset(d, 0, sizeof(d));
d[1][1] = 1;
q.push(make_pair(1, 1));
while(q.size())
{
int xa = q.front().first, xb = q.front().second; q.pop();
for(int i = 0; i < 26; ++i)
{
int ya = sama.ch[xa][i], yb = samb.ch[xb][i];
if(ya&&yb&&!d[ya][yb]) d[ya][yb] = d[xa][xb]+1, q.push(make_pair(ya, yb));
if(ya&&!yb) return d[xa][xb];
}
}
return -1;
}
int bfs2()
{
queue<pair<int, int> > q;
memset(d, 0, sizeof(d));
d[1][1] = 1;
q.push(make_pair(1, 1));
while(q.size())
{
int xa = q.front().first, xb = q.front().second; q.pop();
for(int i = 0; i < 26; ++i)
{
int ya = sama.ch[xa][i], yb = sqamb.ch[xb][i];
if(ya&&yb&&!d[ya][yb]) d[ya][yb] = d[xa][xb]+1, q.push(make_pair(ya, yb));
if(ya&&!yb) return d[xa][xb];
}
}
return -1;
}
int bfs3()
{
queue<pair<int, int> > q;
memset(d, 0, sizeof(d));
d[1][1] = 1;
q.push(make_pair(1, 1));
while(q.size())
{
int xa = q.front().first, xb = q.front().second; q.pop();
for(int i = 0; i < 26; ++i)
{
int ya = sqama.ch[xa][i], yb = samb.ch[xb][i];
if(ya&&yb&&!d[ya][yb]) d[ya][yb] = d[xa][xb]+1, q.push(make_pair(ya, yb));
if(ya&&!yb) return d[xa][xb];
}
}
return -1;
}
int bfs4()
{
queue<pair<int, int> > q;
memset(d, 0, sizeof(d));
d[1][1] = 1;
q.push(make_pair(1, 1));
while(q.size())
{
int xa = q.front().first, xb = q.front().second; q.pop();
for(int i = 0; i < 26; ++i)
{
int ya = sqama.ch[xa][i], yb = sqamb.ch[xb][i];
if(ya&&yb&&!d[ya][yb]) d[ya][yb] = d[xa][xb]+1, q.push(make_pair(ya, yb));
if(ya&&!yb) return d[xa][xb];
}
}
return -1;
}
int main()
{
scanf("%s", s);
for(int i = 0; s[i]; ++i)
sama.insert(s[i]-'a'), sqama.insert(s[i]-'a');
scanf("%s", s);
for(int i = 0; s[i]; ++i)
samb.insert(s[i]-'a'), sqamb.insert(s[i]-'a');
printf("%d\n%d\n%d\n%d\n", bfs1(), bfs2(), bfs3(), bfs4());
return 0;
}