forked from raiden11/Competitive-Programming-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkmp_algorithm.cpp
More file actions
52 lines (48 loc) · 808 Bytes
/
Copy pathkmp_algorithm.cpp
File metadata and controls
52 lines (48 loc) · 808 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
50
51
//kmp
//tushar roy
#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
char a[1000],b[1000];
int i,j,arr[1000];
scanf("%s%s",&a,&b);
i=1, j=0;
arr[0]=0;
while(i<strlen(b))
{
while(b[i]!=b[j]&&j>0)
j=arr[j-1];
if(b[j]==b[i])
{
arr[i]=j+1;
j++;
}
else
arr[i]=0;
i++;
}
i=0, j=0;
int f=0, cnt=0;
while(i<strlen(a))
{
while(a[i]!=b[j]&&j>0)
j=arr[j-1];
if(a[i]==b[j])
{
j++;
i++;
}
else
i++;
if(j==strlen(b))
j=0; // found
}
if(f==1)
cout<<cnt<<endl;
else
cout<<"no"<<endl;
return 0;
}