-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4354.cpp
More file actions
58 lines (46 loc) · 1.21 KB
/
4354.cpp
File metadata and controls
58 lines (46 loc) · 1.21 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
#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[10];
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
int n=0;
cin >> kmp[0].P;
while(kmp[n].P != "."){
kmp[n].make_fail_function();
int ans =0;
int len = kmp[n].P.size();
if(len % (len - kmp[n].fail_function[len-1]) != 0) ans = 1;
else ans = len / (len-kmp[n].fail_function[len-1]);
cout << ans << '\n';
cin >> kmp[++n].P;
}
}