-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path찾기(KMP).cpp
54 lines (53 loc) · 1002 Bytes
/
찾기(KMP).cpp
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
//
//BOJ 1786 찾기
//https://www.acmicpc.net/problem/1786
#include <iostream>
#include <string>
#include <vector>
using namespace std;
vector<int> idx;
int cnt=0;
vector<int> makeTable(string pattern){
int j = 0;
vector<int> table(pattern.size(), 0);
for(int i=1; i<pattern.size(); i++){
while(j>0 && pattern[i]!=pattern[j]){
j = table[j-1];
}
if(pattern[i]==pattern[j]){
table[i] = ++j;
}
}
return table;
}
void KMP(string parent, string pattern){
vector<int> table = makeTable(pattern);
int j = 0;
for(int i=0; i<parent.size(); i++){
while(j>0 && parent[i]!=pattern[j]){
j = table[j-1];
}
if(parent[i]==pattern[j]){
if(j == pattern.size()-1){
cnt++;
idx.push_back(i-pattern.size()+2);
j = table[j];
}
else{
j++;
}
}
}
return;
}
int main(){
string pattern, parent;
getline(cin, parent);
getline(cin, pattern);
KMP(parent, pattern);
cout << cnt << "\n";
for(int i=0; i<idx.size(); i++){
printf("%d ", idx[i]);
}
return 0;
}