Pattern Searching : KMP Algorithm
PROBLEM :
Given a text and a pattern, Find whether the pattern exist in the text or not. If it is present print "found" without quotes else print "not found"
without quotes.
Input:
The first line of input contains an integer T denoting the number of test cases. Each test case consist of a string in 'lowercase' only in a
separate line.
Output:
Print "found" or "not found" in a separate line.
Constraints:
1 = T = 30
1 = |s| = 100
Example:
Input
1
geeksforgeeks
geeks
Output
found
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
#include<string.h>
bool Pattern_Searching_KMT(string ,string ) ;
void Prefix_Table(string ,int [],int ) ;
int main()
{
int t ;
string str1,str2 ;
cin>>t ;
while(t--)
{
cin>>str1 ;
cin>>str2 ;
bool ans=Pattern_Searching_KMT(str1,str2) ;
if(ans)
cout<<"found" ;
else
cout<<"not found" ;
cout<<endl ;
}
return 0;
}
bool Pattern_Searching_KMT(string str1,string str2)
{
int l1,l2 ;
l1=str1.length() ;
l2=str2.length() ;
int arr[l2] ;
Prefix_Table(str2,arr,l2) ;
int i,j ;
i=0;
j=0 ;
while(i<l1)
{
if(str1[i]==str2[j])
{
if(j==l2-1)
return true ;
else
{
i++ ;
j++ ;
}
}
else if(j>0)
j=arr[j-1] ;
else
i++ ;
}
return false ;
}
void Prefix_Table(string str,int arr[],int no)
{
int i,j ;
arr[0]=0 ;
i=1 ;
j=0 ;
while(i<no)
{
if(str[i]==str[j])
{
arr[i]=j+1 ;
i++ ;
j++ ;
}
else if(j>0)
j=arr[j-1] ;
else
{
arr[i]=0 ;
i++ ;
}
}
}
--------------------------------------------------------------------------------
Given a text and a pattern, Find whether the pattern exist in the text or not. If it is present print "found" without quotes else print "not found"
without quotes.
Input:
The first line of input contains an integer T denoting the number of test cases. Each test case consist of a string in 'lowercase' only in a
separate line.
Output:
Print "found" or "not found" in a separate line.
Constraints:
1 = T = 30
1 = |s| = 100
Example:
Input
1
geeksforgeeks
geeks
Output
found
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
#include<string.h>
bool Pattern_Searching_KMT(string ,string ) ;
void Prefix_Table(string ,int [],int ) ;
int main()
{
int t ;
string str1,str2 ;
cin>>t ;
while(t--)
{
cin>>str1 ;
cin>>str2 ;
bool ans=Pattern_Searching_KMT(str1,str2) ;
if(ans)
cout<<"found" ;
else
cout<<"not found" ;
cout<<endl ;
}
return 0;
}
bool Pattern_Searching_KMT(string str1,string str2)
{
int l1,l2 ;
l1=str1.length() ;
l2=str2.length() ;
int arr[l2] ;
Prefix_Table(str2,arr,l2) ;
int i,j ;
i=0;
j=0 ;
while(i<l1)
{
if(str1[i]==str2[j])
{
if(j==l2-1)
return true ;
else
{
i++ ;
j++ ;
}
}
else if(j>0)
j=arr[j-1] ;
else
i++ ;
}
return false ;
}
void Prefix_Table(string str,int arr[],int no)
{
int i,j ;
arr[0]=0 ;
i=1 ;
j=0 ;
while(i<no)
{
if(str[i]==str[j])
{
arr[i]=j+1 ;
i++ ;
j++ ;
}
else if(j>0)
j=arr[j-1] ;
else
{
arr[i]=0 ;
i++ ;
}
}
}
--------------------------------------------------------------------------------
Comments
Post a Comment