-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhiho1015.cpp
More file actions
57 lines (51 loc) · 1.03 KB
/
Copy pathhiho1015.cpp
File metadata and controls
57 lines (51 loc) · 1.03 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
#include<iostream>
#include<string>
#include<vector>
using std::cin;
using std::cout;
using std::string;
using std::vector;
using std::endl;
int main(){
int N, countArray[20];
cin>>N;
for(int i = 0; i < N; i ++){
string str_origin, str_pattern;
cin>>str_pattern;
// 构造NEXT矩阵
vector<int> NEXT;
NEXT.push_back(-1); // 初始化NEXT矩阵
for(int j = 1, k = -1; j < str_pattern.length(); j ++){
while(k > -1 && str_pattern[k + 1] != str_pattern[j]){
k = NEXT[k];
}
if(str_pattern[k+1] == str_pattern[j]){
k ++;
}
NEXT.push_back(k);
}
cin>>str_origin;
// 统计匹配的字符串数目
int j, k = -1, count = 0;
for(j = 0; j < str_origin.length(); j ++){
while(k > -1 && str_pattern[k + 1] != str_origin[j]){
k = NEXT[k];
}
if(str_pattern[k + 1] == str_origin[j]){
k ++;
}
if(k == str_pattern.length() - 1){
count ++;
k = NEXT[k];
}
}
countArray[i] = count;
}
for(int i = 0; i < N; i ++){
cout<<countArray[i];
if(i != N -1){
cout<<endl;
}
}
return 0;
}