-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLPS.cpp
105 lines (80 loc) · 1.76 KB
/
LPS.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
105
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
#define vi vector<int>
#define mii map<int,int>
#define pqb priority_queue<int>
#define pqs priority_queue<int,vi,greater<int> >
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define vi vector<int>
#define mod 1000000007
#define tc(t) int t;cin >> t;while(t--)
#define for0(i , n) for(int i=0;i<n;i++)
#define loop(i , a, b) for(int i=a;i<=b;i++)
#define endl '\n'
void computelps(int prehash[], int n, string s){
int i = 0, j = 1;
prehash[0]=0;
while(j < n){
if(s[i] == s[j]){
prehash[j] = i+1;
i++;
j++;
}else{
if(i != 0){
i = prehash[i-1];
}else{
prehash[j] = 0;
j++;
}
}
}
}
vector<int> prefix_function (string Z) {
int n = (int) Z.length();
vector<int> F (n);
F[0]=0;
for (int i=1; i<n; ++i) {
int j = F[i-1];
while (j > 0 && Z[i] != Z[j])
j = F[j-1];
if (Z[i] == Z[j]) ++j;
F[i] = j;
}
return F;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.txt" , "r" , stdin);
freopen("output.txt" , "w" , stdout);
#endif
string pat,txt;
cin >> txt >> pat;
string s = pat + '$' + txt;
int n = s.size();
int prehash[n];
computelps(prehash,n,s);
vector<int> v = prefix_function(s);
int x = pat.size();
int cnt = 0;
for(int i = 0 ; i < n ; i++){
if(prehash[i] == x){
cnt++;
}
}
int cnt1 = 0;
for(int i = 0 ; i < n ; i++){
if(v[i] == x){
cnt1++;
}
}
cout << cnt << " " << cnt1 << endl;
return 0;
}