-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkmp.cpp
62 lines (57 loc) · 1.18 KB
/
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
52
53
54
55
56
57
58
59
60
61
62
#include <bits/stdc++.h>
using namespace std;
#define endl "\n"
#define IOS ios::sync_with_stdio(0); cin.tie(0);cout.tie(0);
#define int long long
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define clr(a) memset(a,0,sizeof(a))
#define all(x) (x).begin(),(x).end()
#define print(ve) for(auto x:ve)cout<<x<<" ";
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*b)/__gcd(a,b)
#pragma GCC optimize("Ofast")
#pragma GCC ("unroll-loops")
const int mod = 1e8;
const int64_t INF = 2e18;
const int N = 1e5 + 5;
const int ep = 1e-9;
void io(){
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
}
//gives the longest proper prefix which is also suffix
vector<int>prefix_function(string &s){
int n = s.size();
vector<int>pre(n,0);
for(int i=1;i<n;i++){
int j = pre[i-1];
while(j>0 && s[j]!=s[i]){
j = pre[j-1];
}
if(s[i]==s[j]){
j++;
}
pre[i] = j;
}
return pre;
}
void solve(){
string s;
cin>>s;
vector<int>pie = prefix_function(s);
}
int32_t main()
{
IOS;
int t=1;
//cin>>t;
while(t--)
{
solve();
cout<<endl;
}
return 0;
}