-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathkmp.cpp
More file actions
56 lines (56 loc) · 1.66 KB
/
Copy pathkmp.cpp
File metadata and controls
56 lines (56 loc) · 1.66 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
#include <bits/stdc++.h>
using namespace std;
struct Aho {
struct Node{ array<int,26> nx; int link=-1; vector<int> out; Node(){ nx.fill(-1); link=-1; } };
vector<Node> t; Aho(){ t.push_back(Node()); }
int add(const string &s, int id){
int v=0;
for(char ch: s){
int c=ch-'a';
if(t[v].nx[c]==-1){ t[v].nx[c]=t.size(); t.push_back(Node()); }
v=t[v].nx[c];
}
t[v].out.push_back(id);
return v;
}
void build(){
queue<int> q; t[0].link=0;
for(int c=0;c<26;c++){
if(t[0].nx[c]!=-1){ t[t[0].nx[c]].link=0; q.push(t[0].nx[c]); }
else t[0].nx[c]=0;
}
while(!q.empty()){
int v=q.front(); q.pop();
for(int c=0;c<26;c++){
int u=t[v].nx[c];
if(u!=-1){ t[u].link = t[t[v].link].nx[c];
for(int id: t[t[u].link].out) t[u].out.push_back(id);
q.push(u);
} else t[v].nx[c] = t[t[v].link].nx[c];
}
}
}
};
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int p; cin>>p;
vector<string> patterns(p);
for(int i=0;i<p;i++) cin>>patterns[i];
string text; cin>>text;
Aho aho;
for(int i=0;i<p;i++) aho.add(patterns[i], i);
aho.build();
vector<vector<int>> occ(p);
int v=0;
for(int i=0;i<(int)text.size();i++){
int c=text[i]-'a';
v = aho.t[v].nx[c];
for(int id: aho.t[v].out) occ[id].push_back(i - patterns[id].size() + 1);
}
for(int i=0;i<p;i++){
cout<<"Pattern "<<i<<": ";
for(int pos: occ[i]) cout<<pos<<' ';
cout<<"\n";
}
}