Longest Common Substring
PROBLEM :
Given two strings ‘X’ and ‘Y’, find the length of the longest common substring.
Examples :
Input : X = "GeeksforGeeks", y = "GeeksQuiz"
Output : 5
The longest common substring is "Geeks" and is of
length 5.
Input : X = "abcdxyz", y = "xyzabcd"
Output : 4
The longest common substring is "abcd" and is of
length 4.
Input : X = "zxabcdezy", y = "yzabcdezx"
Output : 6
The longest common substring is "abcdez" and is of
length 6.
Input:
First line of the input contains no of test cases T,the T test cases follow.
Each test case consist of 2 space separated integers A and B denoting the size of string X and Y respectively
The next two lines contains the 2 string X and Y.
Output:
For each test case print the length of longest common substring of the two strings .
Constraints:
1<=T<=30
1<=size(X),size(Y)<=100
Example:
Input:
2
6 6
ABCDGH
ACDGHR
3 2
ABC
AC
Output:
4
1
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
#include<string.h>
int Longest_Common_Substring(string ,string ,int ,int ) ;
int main()
{
int t,n1,n2 ;
string str1,str2 ;
cin>>t ;
while(t--)
{
cin>>n1>>n2 ;
cin>>str1>>str2 ;
cout<<Longest_Common_Substring(str1,str2,n1,n2)<<endl ;
}
return 0;
}
int Longest_Common_Substring(string str1,string str2,int n1,int n2)
{
int temp[n1+1][n2+1] ;
int i,j,max ;
max=0 ;
for(i=0;i<=n1;i++)
{
for(j=0;j<=n2;j++)
{
if(i==0||j==0)
temp[i][j]=0 ;
else if(str1[i-1]==str2[j-1])
temp[i][j]=temp[i-1][j-1]+1 ;
else
temp[i][j]=0 ;
max=max>temp[i][j]?max:temp[i][j] ;
}
}
return max ;
}
---------------------------------------------------------------------------------
Given two strings ‘X’ and ‘Y’, find the length of the longest common substring.
Examples :
Input : X = "GeeksforGeeks", y = "GeeksQuiz"
Output : 5
The longest common substring is "Geeks" and is of
length 5.
Input : X = "abcdxyz", y = "xyzabcd"
Output : 4
The longest common substring is "abcd" and is of
length 4.
Input : X = "zxabcdezy", y = "yzabcdezx"
Output : 6
The longest common substring is "abcdez" and is of
length 6.
Input:
First line of the input contains no of test cases T,the T test cases follow.
Each test case consist of 2 space separated integers A and B denoting the size of string X and Y respectively
The next two lines contains the 2 string X and Y.
Output:
For each test case print the length of longest common substring of the two strings .
Constraints:
1<=T<=30
1<=size(X),size(Y)<=100
Example:
Input:
2
6 6
ABCDGH
ACDGHR
3 2
ABC
AC
Output:
4
1
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
#include<string.h>
int Longest_Common_Substring(string ,string ,int ,int ) ;
int main()
{
int t,n1,n2 ;
string str1,str2 ;
cin>>t ;
while(t--)
{
cin>>n1>>n2 ;
cin>>str1>>str2 ;
cout<<Longest_Common_Substring(str1,str2,n1,n2)<<endl ;
}
return 0;
}
int Longest_Common_Substring(string str1,string str2,int n1,int n2)
{
int temp[n1+1][n2+1] ;
int i,j,max ;
max=0 ;
for(i=0;i<=n1;i++)
{
for(j=0;j<=n2;j++)
{
if(i==0||j==0)
temp[i][j]=0 ;
else if(str1[i-1]==str2[j-1])
temp[i][j]=temp[i-1][j-1]+1 ;
else
temp[i][j]=0 ;
max=max>temp[i][j]?max:temp[i][j] ;
}
}
return max ;
}
---------------------------------------------------------------------------------
Comments
Post a Comment