-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFST.cpp
More file actions
72 lines (66 loc) · 1.56 KB
/
Copy pathFST.cpp
File metadata and controls
72 lines (66 loc) · 1.56 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
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Alekseev_Aleksei_FST {
protected:
string sub;
int state = 0;
public:
vector<int> prefix;
Alekseev_Aleksei_FST(string sub) : sub(sub) {}
vector<int> prefix_(string s) {
int n = s.length();
vector<int> pi(n, 0);
for (int i = 0; i < n; i++) {
for (int j = 0; j < i; j++) {
if (s.substr(0, j + 1) == s.substr(i - j, j + 1)) {
pi[i] = j + 1;
}
}
}
return pi;
}
int process(char c) {
while (state > 0 && c != sub[state]) {
state = prefix[state - 1];
}
state = transition(c);
if (state == sub.length()) {
state = prefix[state - 1];
return 1;
}
return 0;
}
int transition(char c) {
if (c == this->sub[this->state]) {
this->state++;
} else {
this->state = 0;
}
return this->state;
}
};
int main() {
string str;
getline(cin, str);
Alekseev_Aleksei_FST fst(str);
string str2;
getline(cin, str2);
vector<char> line;
for (char c : str2) {
line.push_back(c);
}
int output = 0;
fst.prefix = fst.prefix_(str2);
for (int i = 0; i < line.size(); i++) {
if (fst.process(line[i]) == 1) {
output++;
}
}
if (output == 0) {
cout << "NOT DETECTED" << endl;
} else {
cout << output << endl;
}
}