-
Notifications
You must be signed in to change notification settings - Fork 1
/
LightOJ 1255.cpp
104 lines (79 loc) · 1.47 KB
/
LightOJ 1255.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <bits/stdc++.h>
using namespace std;
#define READ(f) freopen(f, "r", stdin)
#define WRITE(f) freopen(f, "w", stdout)
#define rep(i, n) for(int i = 0 ; i < (n) ; i++)
#define iter(i, a, b) for(int i = (a) ; i < (b) ; i++)
#define mem(a, b) memset(a, b, sizeof(a))
typedef long long ll;
typedef pair< int , int > pii;
typedef pair< int , pii > iii;
typedef vector<int> vi;
typedef vector< pii > vii;
typedef map<int, int> mii;
#define INF (1 << 29)
#define PI (2.0 * acos(0.0))
#define MAXX 1000010
int failure[MAXX];
ll cnt;
void init(){
cnt = 0;
rep(i, MAXX)
failure[i] = 0;
}
void KMP_Preprocess(string pattern, int m){
failure[0] = 0;
failure[1] = 0;
for(int i = 2; i <= m; i++){
int j = failure[i - 1];
while(true){
if(pattern[j] == pattern[i - 1]){
failure[i] = j + 1;
break;
}
if(j == 0){
failure[i] = 0;
break;
}
j = failure[j];
}
}
}
void KMP(string text, string pattern){
int n = text.size();
int m = pattern.size();
KMP_Preprocess(pattern, m);
int i = 0, j = 0;
while(true){
if(j == n)
return;
if(text[j] == pattern[i]){
i++;
j++;
if(i == m)
cnt++;
}
else{
if(i == 0)
j++;
else
i = failure[i];
}
}
return;
}
int main(){
//READ("LightOJ 1255.txt");
//WRITE("Out.txt");
int tc, case_no = 0;
scanf("%d", &tc);
string text, pattern;
while(tc--){
init();
cin >> text;
cin >> pattern;
KMP(text, pattern);
printf("Case %d: %lld\n", ++case_no, cnt);
}
return 0;
}