This repository was archived by the owner on Feb 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMutliple_String_Matching.cpp
More file actions
96 lines (96 loc) · 3.06 KB
/
Mutliple_String_Matching.cpp
File metadata and controls
96 lines (96 loc) · 3.06 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
// Multiple string matching using Aho-Corasixk algortihm
#include <iostream>
#include <vector>
#include <queue>
#include <unordered_map>
using namespace std;
class AhoCorasick {
private:
struct TrieNode {
unordered_map<char, TrieNode*> children;
TrieNode* fail;
vector<int> output;
TrieNode() : fail(nullptr) {}
};
TrieNode* root;
vector<int> patternLengths;
public:
AhoCorasick() {
root = new TrieNode();
}
void insert(const string& pattern, int patternIndex) {
TrieNode* node = root;
for (char ch : pattern) {
if (node->children.find(ch) == node->children.end()) {
node->children[ch] = new TrieNode();
}
node = node->children[ch];
}
node->output.push_back(patternIndex);
patternLengths.push_back(pattern.length());
}
void buildFailureLinks() {
queue<TrieNode*> q;
for (auto& pair : root->children) {
pair.second->fail = root;
q.push(pair.second);
}
while (!q.empty()) {
TrieNode* current = q.front();
q.pop();
for (auto& pair : current->children) {
char ch = pair.first;
TrieNode* child = pair.second;
q.push(child);
TrieNode* failLink = current->fail;
while (failLink != nullptr && failLink->children.find(ch) == failLink->children.end()) {
failLink = failLink->fail;
}
child->fail = (failLink != nullptr) ? failLink->children[ch] : root;
child->output.insert(child->output.end(), child->fail->output.begin(), child->fail->output.end());
}
}
}
vector<int> search(const string& text) {
vector<int> result;
TrieNode* currentState = root;
for (int i = 0; i < text.length(); ++i) {
char ch = text[i];
while (currentState != nullptr && currentState->children.find(ch) == currentState->children.end()) {
currentState = currentState->fail;
}
if (currentState == nullptr) {
currentState = root;
} else {
currentState = currentState->children[ch];
for (int index : currentState->output) {
result.push_back(i - patternLengths[index] + 1);
}
}
}
return result;
}
};
int main() {
AhoCorasick ac;
int numPatterns;
cout << "Enter the number of patterns: ";
cin >> numPatterns;
vector<string> patterns(numPatterns);
for (int i = 0; i < numPatterns; ++i) {
cout << "Enter pattern " << i + 1 << ": ";
cin >> patterns[i];
ac.insert(patterns[i], i);
}
ac.buildFailureLinks();
string text;
cout << "Enter the text: ";
cin >> text;
vector<int> matches = ac.search(text);
cout << "Pattern matches found at positions: ";
for (int match : matches) {
cout << match << " ";
}
cout << endl;
return 0;
}