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 pathSuffix_Automation.cpp
More file actions
79 lines (79 loc) · 2.72 KB
/
Suffix_Automation.cpp
File metadata and controls
79 lines (79 loc) · 2.72 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
#include <iostream>
#include <vector>
#include <unordered_map>
class SuffixAutomaton {
private:
struct State {
std::unordered_map<char, int> transitions;
int length;
int link;
State() : length(0), link(-1) {}
};
std::vector<State> states;
int lastState;
public:
SuffixAutomaton() {
states.push_back(State());
lastState = 0;
}
void addString(const std::string& str) {
for (char c : str) {
extend(c);
}
}
bool isSubstring(const std::string& str) {
int currentState = 0;
for (char c : str) {
if (states[currentState].transitions.find(c) == states[currentState].transitions.end()) {
return false;
}
currentState = states[currentState].transitions[c];
}
return true;
}
private:
void extend(char c) {
int newStateIndex = states.size();
states.push_back(State());
int currentState = lastState;
states[newStateIndex].length = states[currentState].length + 1;
while (currentState != -1 && states[currentState].transitions.find(c) == states[currentState].transitions.end()) {
states[currentState].transitions[c] = newStateIndex;
currentState = states[currentState].link;
}
if (currentState == -1) {
states[newStateIndex].link = 0;
} else {
int nextState = states[currentState].transitions[c];
if (states[currentState].length + 1 == states[nextState].length) {
states[newStateIndex].link = nextState;
} else {
int cloneStateIndex = states.size();
states.push_back(states[nextState]);
states[cloneStateIndex].length = states[currentState].length + 1;
while (currentState != -1 && states[currentState].transitions[c] == nextState) {
states[currentState].transitions[c] = cloneStateIndex;
currentState = states[currentState].link;
}
states[nextState].link = cloneStateIndex;
states[newStateIndex].link = cloneStateIndex;
}
}
lastState = newStateIndex;
}
};
int main() {
SuffixAutomaton automaton;
automaton.addString("Football");
automaton.addString("Boxing");
automaton.addString("Weightlifting");
std::string query;
std::cout << "Enter a string to check if it is a substring: ";
std::cin >> query;
if (automaton.isSubstring(query)) {
std::cout << "The entered string is a substring." << std::endl;
} else {
std::cout << "The entered string is not a substring." << std::endl;
}
return 0;
}