-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkmp.cpp
51 lines (47 loc) · 892 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
#include<iostream>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<map>
#include<string>
using namespace std;
#define REP(i,n) for(int i=0;i<(int)n;++i)
vector<int> get_matches(string hay, string needle) {
int n=needle.size();
vector<int> rval(hay.size());
vector<int> jump(n+1);
int j=-1;
int i=0;
jump[0]=-1;
while(i<n) {
if(j>=0 && needle[i]!=needle[j]) j=jump[j];else jump[++i]=++j;
}
i=0;
j=-1;
while(j<hay.size()) {
while(j>=0 && a[j] !=hay[i]) j=jump[j];
j++;
if(j==n) {
rval[i-j+1]=1;
j=jump[j];
}
++i;
}
return rval;
}
int main() {
int n,m,k;
cin>>n>>m>>k;
string s,t;cin>>s>>t;
vector<int> v[4];
char c[4]={'A','C','T','G'};
REP(i,4) {
v[i]=get_matches(s,t,c[i],k);
}
int rval=0;
REP(i,n) {
rval+=v[0][i]&v[1][i]&v[2][i]&v[3][i];
}
cout<<rval<<endl;
}