-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAhoCorasick.cpp.save
More file actions
167 lines (155 loc) · 3.81 KB
/
AhoCorasick.cpp.save
File metadata and controls
167 lines (155 loc) · 3.81 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 300005, sigma = 26;
struct AhoCorasick
{
struct State
{
int to[sigma];
int go[sigma];
int par;
int suffLink;
int cnt;
int toLet; //via whitch letter did this node came
int depth;
};
vector<State> node;
//State node[MAX];
int nodeCount;
int root;
vector< vector<int> > suffRG;
inline int index( char ch )
{
return ch - 'A';
}
inline void create( int i )
{
//suffRG[i].clear();
node[i].cnt = 0;
for( int j = 0; j < sigma; j++ )
node[i].to[j] = node[i].go[j] = -1;
}
void init( int n = MAXN )
{
node.resize(n+1);
suffRG.resize(n+1);
nodeCount = 0;
create(++nodeCount);
root = 1;
node[root].depth = 0;
node[root].par = 0;
for( int i = 0; i < n; i++ )
suffRG[i].clear();
}
int add(const char *str ) //returns the node it is added
{
int cur = root;//, len = strlen(str);
for( int i = 0; str[i]; i++ )
{
int v = index(str[i]);
if( node[cur].to[v] == -1 )
{
create(++nodeCount);
node[cur].to[v] = nodeCount;
node[nodeCount].par = cur;
node[nodeCount].depth = node[cur].depth+1;
}
cur = node[cur].to[v];
node[cur].toLet = v;
}
return cur;
}
void suffLink()
{
int cur = root;
queue<int>q;
q.push(cur);
while( !q.empty() )
{
int u = q.front();
q.pop();
int last = node[u].toLet;
if( node[u].depth <= 1 )
{
node[u].suffLink = 1;
}
else
{
int cur = node[ node[u].par ].suffLink;
while( cur > 1 && node[cur].to[last] == -1 )
{
cur = node[cur].suffLink;
}
if( node[cur].to[last] != -1)
cur = node[cur].to[last];
node[u].suffLink = cur;
}
if( u != 1 )
{
suffRG[node[u].suffLink].push_back(u);
}
for( int i = 0; i < sigma; i++ )
{
if( node[u].to[i] != -1 )
q.push(node[u].to[i]);
}
}
}
inline int nextState(int u, int v)
{
if( node[u].go[v] != -1 )
return node[u].go[v];
if( node[u].to[v] != -1 )
return node[u].go[v] = node[u].to[v];
if( u == 1)
return node[u].go[v] = max( node[u].to[v], 1 );
return node[u].go[v] = nextState(node[u].suffLink, v);
}
void dfs( int u, int p )
{
for( int i = 0; i < suffRG[u].size(); i++ )
{
int v = suffRG[u][i];
if( v == p ) continue;
dfs(v, u);
node[u].cnt += node[v].cnt;
}
}
};
char str[1000005];
char s[505];
int ind[505];
int main()
{
//freopen("input.txt", "r", stdin);
int t, caseno = 1;
scanf("%d", &t);
AhoCorasick ac;
while(t--)
{
ac.init(505*508);
int n;
scanf("%d", &n);
scanf("%s", str);
for( int i = 1; i <= n; i++ )
{
scanf("%s", s);
ind[i] = ac.add(s);
}
ac.suffLink();
int u = ac.root;
for( int i = 0; str[i]; i++ )
{
int v = str[i] - 'a';
u = ac.nextState(u,v);
ac.node[u].cnt++;
}
ac.dfs(1,-1);
printf("Case %d:\n", caseno++);
for( int i = 1; i <= n; i++ )
{
printf("%d\n", ac.node[ind[i]].cnt);
}
}
return 0;
}