Longest Common substring of a string

Source: Internet
Author: User

[Cpp]
// Longest public subsequence problem. cpp: Defines the entry point for the console application.
//
/* Problem: two strings are provided to find out their longest common subsequences.
What is the longest common subsequence?
Longest Common Subsequence, abbreviated as LCS (Longest Common Subsequence ).
It is defined as a sequence S. If it is a subsequence of two or more known sequences,
S is the longest common subsequence of all known sequences that meet the longest condition sequence.
The longest public substrings (requiring continuous) and longest public substrings are different (can be discontinuous)
For example:
Abgfjlmnp
----
Afkqln
----
Their Longest Common subsequences are: afln
Ideas:
Dynamic Planning
Set two subsequences X = {x1, x2, x3,... xi}, Y = {y1, y2, y3,..., yi}
Set C [I, j] To save the LCS length of Xi and Yj (I = 0, 1... j = 0 ,...)
Recursive equations can be obtained:
__
_ | 0 I = 0 or j = 0
C [I, j] = | _ C [I-1, J-1] + 1 I, j> 0 and xi = yi
|__ Max {C [I, J-1], C [I-1, j]} I, j> 0 and xi! = Yi
 
According to the formula, we can know that C [I, j] stores the maximum length of the current (Xi, Yi) subsequence.
Knowing the length of the longest common subsequence, the next step is to consider how to output the sequence.
To output sub-sequences, we need to add an array pos [I, j]
Pos [I, j] is used to save the solution of C [I, j ].
There are three scenarios:
1:
C [I, j]: = c [I-1, J-1] + 1;
Pos [I, j]: = "";
2:
C [I, j]: = c [I-1, j];
Pos [I, j]: = "random ";
3:
C [I, j]: = c [I, J-1];
Pos [I, j]: = "random"
When constructing subsequences:
Start scanning from pos [m, n:
1. When the pos [I, j] Encounters "" (meaning xi = yi is an element of LCS ),
Indicates that the longest common subsequence of Xi and Yj is the subsequence obtained by adding xi at the end of the longest common subsequence of Xi-1 and Yj-1;
2. When "cosine" is encountered in pos [I, j], it indicates that the longest common subsequence of Xi and Yj is the same as the longest common subsequence of Xi-1 and Yj;
3. When "substring" is encountered in pos [I, j], it indicates that the longest common subsequence of Xi and Yj is the same as the longest common subsequence of Xi and Yj-1.

*/
# Include "stdafx. h"
# Include <iostream>
Using namespace std;
Void ConstructLCS (int ** pos, const char * str, int length1, int leng22 );
Void LCS (const char * str1, const char * str2, int length1, int leng22)
{
// Initialize and dynamically create two-dimensional arrays
Int ** c = new int * [length1 + 1];
Int ** pos = new int * [length1 + 1];
For (int I = 0; I <length1 + 1; I ++)
{
C [I] = new int [length1 + 1];
Pos [I] = new int [length1 + 1];
}
For (int I = 0; I <length1 + 1; I ++)
C [I] [0] = 0;
For (int j = 0; j <length1 + 1; j ++)
C [0] [j] = 0;

// 0 indicates
// 1 indicates Region
// 2 stands for callback
For (int I = 1; I <= length1; I ++)
For (int j = 1; j <= leng2; j ++)
{
If (str1 [I-1] = str2 [J-1])
{
C [I] [j] = c [I-1] [J-1] + 1;
Pos [I] [j] = 0;
}
Else if (c [I-1] [j]> = c [I] [J-1])
{
C [I] [j] = c [I-1] [j];
Pos [I] [j] = 1;
}
Else
{
C [I] [j] = c [I] [J-1];
Pos [I] [j] = 2;
}
}
Cout <"Longest Common Sub-sequence length:" <c [length1] [length1] <endl;
Cout <"The Longest Common subsequence is :";
ConstructLCS (pos, str1, length1, leng22 );
Cout <endl;
}
// Construct the oldest Sequence
Void ConstructLCS (int ** pos, const char * str, int length1, int leng22)
{
If (length1 = 0 | length1 = 0)
Return;
If (pos [length1] [length1] = 0)
{
ConstructLCS (pos, str, length1-1, length2-1 );
Cout <str [length1-1];
}
Else if (pos [length1] [length1] = 1)
ConstructLCS (pos, str, length1-1, leng22 );
Else if (pos [length1] [length1] = 2)
ConstructLCS (pos, str, length1, length2-1 );
}
 
Int _ tmain (int argc, _ TCHAR * argv [])
{
Char * str1 = "abcefghkjl ";
Char * str2 = "bfhjkjl ";
LCS (str1, str2, 10, 7 );
System ("pause ");
Return 0;
}

// Longest public subsequence problem. cpp: Defines the entry point for the console application.
//
/* Problem: two strings are provided to find out their longest common subsequences.
What is the longest common subsequence?
Longest Common Subsequence, abbreviated as LCS (Longest Common Subsequence ).
It is defined as a sequence S. If it is a subsequence of two or more known sequences,
S is the longest common subsequence of all known sequences that meet the longest condition sequence.
The longest public substrings (requiring continuous) and longest public substrings are different (can be discontinuous)
For example:
Abgfjlmnp
----
Afkqln
----
Their Longest Common subsequences are: afln
Ideas:
Dynamic Planning
Set two subsequences X = {x1, x2, x3,... xi}, Y = {y1, y2, y3,..., yi}
Set C [I, j] To save the LCS length of Xi and Yj (I = 0, 1... j = 0 ,...)
Recursive equations can be obtained:
__
_ | 0 I = 0 or j = 0
C [I, j] = | _ C [I-1, J-1] + 1 I, j> 0 and xi = yi
|__ Max {C [I, J-1], C [I-1, j]} I, j> 0 and xi! = Yi

According to the formula, we can know that C [I, j] stores the maximum length of the current (Xi, Yi) subsequence.
Knowing the length of the longest common subsequence, the next step is to consider how to output the sequence.
To output sub-sequences, we need to add an array pos [I, j]
Pos [I, j] is used to save the solution of C [I, j ].
There are three scenarios:
1:
C [I, j]: = c [I-1, J-1] + 1;
Pos [I, j]: = "";
2:
C [I, j]: = c [I-1, j];
Pos [I, j]: = "random ";
3:
C [I, j]: = c [I, J-1];
Pos [I, j]: = "random"
When constructing subsequences:
Start scanning from pos [m, n:
1. When the pos [I, j] Encounters "" (meaning xi = yi is an element of LCS ),
Indicates that the longest common subsequence of Xi and Yj is the subsequence obtained by adding xi at the end of the longest common subsequence of Xi-1 and Yj-1;
2. When "cosine" is encountered in pos [I, j], it indicates that the longest common subsequence of Xi and Yj is the same as the longest common subsequence of Xi-1 and Yj;
3. When "substring" is encountered in pos [I, j], it indicates that the longest common subsequence of Xi and Yj is the same as the longest common subsequence of Xi and Yj-1.

*/
# Include "stdafx. h"
# Include <iostream>
Using namespace std;
Void ConstructLCS (int ** pos, const char * str, int length1, int leng22 );
Void LCS (const char * str1, const char * str2, int length1, int leng22)
{
// Initialize and dynamically create two-dimensional arrays
Int ** c = new int * [length1 + 1];
Int ** pos = new int * [length1 + 1];
For (int I = 0; I <length1 + 1; I ++)
{
C [I] = new int [length1 + 1];
Pos [I] = new int [length1 + 1];
}
For (int I = 0; I <length1 + 1; I ++)
C [I] [0] = 0;
For (int j = 0; j <length1 + 1; j ++)
C [0] [j] = 0;
 
// 0 indicates
// 1 indicates Region
// 2 stands for callback
For (int I = 1; I <= length1; I ++)
For (int j = 1; j <= leng2; j ++)
{
If (str1 [I-1] = str2 [J-1])
{
C [I] [j] = c [I-1] [J-1] + 1;
Pos [I] [j] = 0;
}
Else if (c [I-1] [j]> = c [I] [J-1])
{
C [I] [j] = c [I-1] [j];
Pos [I] [j] = 1;
}
Else
{
C [I] [j] = c [I] [J-1];
Pos [I] [j] = 2;
}
}
Cout <"Longest Common Sub-sequence length:" <c [length1] [length1] <endl;
Cout <"The Longest Common subsequence is :";
ConstructLCS (pos, str1, length1, leng22 );
Cout <endl;
}
// Construct the oldest Sequence
Void ConstructLCS (int ** pos, const char * str, int length1, int leng22)
{
If (length1 = 0 | length1 = 0)
Return;
If (pos [length1] [length1] = 0)
{
ConstructLCS (pos, str, length1-1, length2-1 );
Cout <str [length1-1];
}
Else if (pos [length1] [length1] = 1)
ConstructLCS (pos, str, length1-1, leng22 );
Else if (pos [length1] [length1] = 2)
ConstructLCS (pos, str, length1, length2-1 );
}

Int _ tmain (int argc, _ TCHAR * argv [])
{
Char * str1 = "abcefghkjl ";
Char * str2 = "bfhjkjl ";
LCS (str1, str2, 10, 7 );
System ("pause ");
Return 0;
}

 
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.