-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathString_Matching.cpp
More file actions
63 lines (55 loc) · 1.06 KB
/
Copy pathString_Matching.cpp
File metadata and controls
63 lines (55 loc) · 1.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
#include <bits/stdc++.h>
using namespace std;
long long ans;
vector<int> next_array(const string &p) {
int m =p.length();
vector<int> next (m+1,0);
if (m==1) {
next[0] = 0;
return next;
}
next[0] = -1;
next[1] = 0;
int i = 2, cn = 0;
while (i <= m) {
if (p[i - 1] == p[cn]) {
next[i++] = ++cn;
} else if (cn > 0) {
cn = next[cn];
} else {
next[i++] = 0;
}
}
return next;
}
void kmp(const string &n, const string &m) {
vector<int> next =next_array(m);
int len1=n.length(),len2=m.length();
int x=0,y=0;
while (x < len1) {
if (n[x] == m[y]) {
x++;
y++;
} else if (y == 0) {
x++;
} else {
y = next[y];
}
if (y == len2) {
ans++;
y = next[y];
}
}
}
void solve() {
string n,m;
cin>>n>>m;
kmp(n,m);
cout<<ans<<endl;
}
int main () {
ios::sync_with_stdio(0);
cin.tie(0);
solve();
return 0;
}