我是靠谱客的博主 故意犀牛,这篇文章主要介绍hsy单词,现在分享给大家,希望可以做个参考。

题意:略

在ac自动机上,一个节点出现的次数等于能通过fail到它的节点的次数之和。而叶节点就等于它被爬过的次数。

复制代码
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
124
125
126
127
128
#include <iostream> #include <cstdio> #include <cmath> #include <algorithm> #include <vector> #include <iomanip> #include <cstring> #include <map> #include <queue> #include <set> #include <cassert> #include <stack> #include <bitset> #define mkp make_pair using namespace std; const double EPS=1e-12; typedef long long lon; const lon SZ=1000010,SSZ=51,APB=26,one=1,INF=0x7FFFFFFF,mod=1000000007; int n,nex[SZ][APB],cnt,num[SZ]; int fail[SZ],match[SZ],in[SZ],ans[SZ]; char ch[SZ]; void build(int x) { int cur=0; for(int i=1;ch[i];++i) { int c=ch[i]-'a'; if(!nex[cur][c])nex[cur][c]=++cnt; cur=nex[cur][c]; ++num[cur]; //cout<<num[cur]<<endl; } match[x]=cur; } void build_fail() { queue<int> q; q.push(0); for(;q.size();) { int fr=q.front(); q.pop(); for(int i=0;i<APB;++i) { if(nex[fr][i]) { int u=nex[fr][i]; if(fr==0) { fail[u]=0; } else { int v=fail[fr]; for(;!nex[v][i]&&v;v=fail[v]); if(nex[v][i])fail[u]=nex[v][i]; else fail[u]=0; } q.push(u); } } } } void topo() { for(int i=1;i<=cnt;++i) { ++in[fail[i]]; } stack<int> stk; for(int i=1;i<=cnt;++i) { if(!in[i]) { stk.push(i); //cout<<"i: "<<i<<endl; } ans[i]=num[i]; } for(;stk.size();) { int top=stk.top(); stk.pop(); --in[fail[top]],ans[fail[top]]+=ans[top]; if(!in[fail[top]]) { stk.push(fail[top]); } } } void init() { cin>>n; for(int i=1;i<=n;++i) { cin>>ch+1; build(i); } build_fail(); topo(); for(int i=1;i<=n;++i) { cout<<ans[match[i]]<<endl; } } void work() { } int main() { std::ios::sync_with_stdio(0); //freopen("d:\1.txt","r",stdin); int casenum; //cin>>casenum; //cout<<casenum<<endl; //for(int time=1;time<=casenum;++time) //for(int time=1;cin>>n>>qnum,n;++time) { //cout<<"Case "<<time<<": "; init(); work(); } return 0; }

 

转载于:https://www.cnblogs.com/gaudar/p/10740369.html

最后

以上就是故意犀牛最近收集整理的关于hsy单词的全部内容,更多相关hsy单词内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(88)

评论列表共有 0 条评论

立即
投稿
返回
顶部