-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1305.cpp
More file actions
49 lines (39 loc) · 1000 Bytes
/
1305.cpp
File metadata and controls
49 lines (39 loc) · 1000 Bytes
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
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
#define MAXN 1000050
struct KMP{
string S,P;
int hit_count = 0;
int fail_function[MAXN]={0,};
vector<int> index;
void make_fail_function(){
int j=0;
for(int i=1 ; i<P.size() ; i++){
while(j>0 && P[i] != P[j]) j = fail_function[j-1];
if(P[i] == P[j]) fail_function[i] = ++j;
}
}
void do_KMP(){
int j=0;
for(int i=0 ; i<S.size() ; i++){
while(j>0 && S[i] != P[j]) j = fail_function[j-1];
if(S[i] == P[j]) j++;
if(j == P.size()){
hit_count++;
index.push_back(i-j+1);
j = fail_function[j-1];
}
}
}
}kmp;
int L;
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
cin >> L >> kmp.P;
kmp.make_fail_function();
cout << L-kmp.fail_function[kmp.P.size()-1];
}