隐藏
「SDOI2008」Sandy的卡片 - KMP暴力/SAM/后缀数组 | Bill Yang's Blog

路终会有尽头,但视野总能看到更远的地方。

0%

「SDOI2008」Sandy的卡片 - KMP暴力/SAM/后缀数组

题目大意

    对经过差分的数组求多串LCS。


题目分析

因为数据范围小,本题做法很多。
可以直接上暴力KMP。
可以使用后缀数组。
可以使用SAM。

本处使用SAM跑多串LCS。
是一道模板题,学习笔记中总结过。


代码

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
123
#include<algorithm>
#include<iostream>
#include<iomanip>
#include<cstring>
#include<cstdlib>
#include<vector>
#include<cstdio>
#include<cmath>
#include<queue>
#include<map>
using namespace std;

inline const int Get_Int() {
int num=0,bj=1;
char x=getchar();
while(x<'0'||x>'9') {
if(x=='-')bj=-1;
x=getchar();
}
while(x>='0'&&x<='9') {
num=num*10+x-'0';
x=getchar();
}
return num*bj;
}

const int maxn=205,maxc=10;
int Bucket[maxn*2],top[maxn*2],tmp[maxn*2],Min[maxn*2];

struct SuffixAutomaton {
int cnt,root,last;
int next[maxn*2],Max[maxn*2],end_pos[maxn*2];
map<int,int>child[maxn];
SuffixAutomaton() {
cnt=0;
root=last=newnode(0);
}
int newnode(int val) {
cnt++;
next[cnt]=end_pos[cnt]=0;
Max[cnt]=val;
child[cnt].clear();
return cnt;
}
void insert(int data) {
int p=last,u=newnode(Max[last]+1);
last=u;
for(; p&&!child[p][data]; p=next[p])child[p][data]=u;
if(!p)next[u]=root;
else {
int old=child[p][data];
if(Max[old]==Max[p]+1)next[u]=old;
else {
int New=newnode(Max[p]+1);
child[New]=child[old];
next[New]=next[old];
next[u]=next[old]=New;
for(; child[p][data]==old; p=next[p])child[p][data]=New;
}
}
}
void build(vector<int> a) {
for(auto x:a)insert(x);
}
void topsort() {
for(int i=1; i<=cnt; i++)Bucket[Max[i]]++;
for(int i=1; i<=cnt; i++)Bucket[i]+=Bucket[i-1];
for(int i=1; i<=cnt; i++)top[Bucket[Max[i]]--]=i;
}
void lcs(vector<int> a) {
memset(tmp,0,sizeof(tmp));
int len=0,p=root;
for(auto ch:a) {
if(child[p][ch])p=child[p][ch],len++;
else {
while(p&&!child[p][ch])p=next[p];
if(!p) {
len=0;
p=root;
} else {
len=Max[p]+1;
p=child[p][ch];
}
}
tmp[p]=max(tmp[p],len);
}
for(int i=cnt; i>=1; i--) { //逆拓扑序
int Now=top[i];
Min[Now]=min(Min[Now],tmp[Now]);
if(tmp[Now]&&next[Now])tmp[next[Now]]=Max[next[Now]];
}
}
} sam;

int t;
vector<int> a;

int main() {
t=Get_Int();
int l=Get_Int(),last=0;
for(int i=1; i<=l; i++) {
int x=Get_Int();
a.push_back(x-last);
last=x;
}
sam.build(a);
for(int i=1; i<=sam.cnt; i++)Min[i]=sam.Max[i];
sam.topsort();
for(int i=1; i<t; i++) {
a.clear();
int l=Get_Int(),last=0;
for(int i=1; i<=l; i++) {
int x=Get_Int();
a.push_back(x-last);
last=x;
}
sam.lcs(a);
}
int ans=0;
for(int i=1; i<=sam.cnt; i++)ans=max(ans,Min[i]);
printf("%d\n",ans+1);
return 0;
}
姥爷们赏瓶冰阔落吧~