(1) 找出兩個字符串的最長公共子串
題目:輸入兩個字符串,找出兩個字符串中最長的公共子串。
找兩個字符串的最長公共子串,這個子串要求在原字符串中是連續的。因此我們採用一個二維矩陣來存儲中間結果,下面我們看這個二維數組如何構造?
假設兩個字符串分別是:”bab”和”caba”。
如果str[i] == str[j] 則matrix[i][j] = 1,否則matrix[i][j] = 0
然後我們從矩陣中找出斜對角線最長的那個子字符串,就是最長公共子串。
即”ab”和”ba”分別爲2。
我們可以簡化一下,在當我們計算matrix[i][j]時,我們判斷str[i] == str[j] 和matrix[i-1][j-1]。
如果str[i] == str[j],則matrix[i][j] = matrix[i-1][j-1] + 1;否則matrix[i][j] = 0。
如下圖所示:
所以此時,我們只是將matrix[M][N]中,找到最大的值,即爲最長公共子串。
然後我們還可以簡化一下空間複雜度。
因爲我們每判斷一個matrix[i][j]時,實際上它只與matrix[i-1][j-1]相關。故所以我們可以使用一維數組來保存上一次的結果。
實現代碼如下:
#include <cstring>
#include <iostream>
using namespace std;
int GetLongestCommonSubString(const char *pStr1, const char *pStr2)
{
/* 判斷參數合法性 */
if (pStr1 == NULL || pStr2 == NULL)
{
return -1;
}
int n = strlen(pStr1);
int m = strlen(pStr2);
int longestCommonSubString = 0;
/* 申請輔助空間,並初始化爲0 */
int *LCS = new int[m];
for (int i = 0; i < m; i++)
{
LCS[i] = 0;
}
/* 不斷判斷pStr[i] ?= pStr[j],然後根據不同情況來更新LCS */
for (int i = 0; i < n; i++)
{
for (int j = m - 1; j >= 0; j--)
{
if (pStr1[i] == pStr2[j]) /* 如果pStr1[i] == pStr2[j],LCS[j] = LCS[j-1] + 1 */
{
if (j == 0)
{
LCS[j] = 1;
}
else
{
LCS[j] = LCS[j-1] + 1;
}
}
else /* 如果pStr1[i] != pStr2[j],LCS[j] = 0 */
{
LCS[j] = 0;
}
/* 更新最長子串的長度 */
if (LCS[j] > longestCommonSubString)
{
longestCommonSubString = LCS[j];
}
}
}
delete LCS;
LCS = NULL;
return longestCommonSubString;
}
void Test(const char *testName, const char *pStr1, const char *pStr2, int expectedLongestCommonSubString)
{
cout << testName << " : ";
if (GetLongestCommonSubString(pStr1, pStr2) == expectedLongestCommonSubString)
{
cout << "Passed." << endl;
}
else
{
cout << "Failed." << endl;
}
}
int main()
{
Test("Test1", "caba", "bab", 2);
Test("Test2", "abcd", "efg", 0);
Test("Test3", "abcde", "abcde", 5);
}
(2) 找出兩個字符串的最長公共子序列
題目:輸入兩個字符串,求兩個字符串的最長公共子序列。
首先,最長公共子序列與最長公共子串不同,子序列不要求其在原字符串是連續的。例如字符串X={A,B,C,B,D,A,B},Y = {B,D,C,A,B,A},則X與Y的最長公共子序列爲Z={B,C,B,A}。
我們假設X={x1, x2, x3, …, xm},則X的前綴,Xi = {x1, x2, … ,xi}。即X={A,B,C,B,D,A,B},X4={A,B,C,B}。
Y = {y1, y2, y3, … ,yn},則Z={z1, z2, …,zk} 是X和Y的最長公共子序列。
如果xm == yn, 則zk = xm =yn 並且 Zk-1 是Xm-1 和 Yn-1的最長公共子序列。
如果 xm != yn, 則zk != xm,並且Z是Xm-1和Yn的最長公共子序列。
如果 xm != yn, 則zk != yn,並且Z是xm 和Yn-1的最長公共子序列。
所以我們定義了C[i][j]二維數組,用來存儲Xi和Yj的最長公共子序列。
0 如果i==0或者j==0
即C[i][j] = c[i-1][j-1] + 1 如果i,j > 0並且 xi == yj
Max(c[i][j-1],c[i-1][j]) 如果i,j > 0 並且xi != yj
實現代碼如下:
#include <cstdio>
#include <iostream>
using namespace std;
int max(int a, int b)
{
return a > b ? a : b;
}
int GetLongestCommonSequence(const char *pStr1, const char *pStr2)
{
/* 判斷參數的合法性 */
if (pStr1 == NULL || pStr2 == NULL)
{
return -1;
}
int m = strlen(pStr1);
int n = strlen(pStr2);
/* 申請二維空間LCS[m+1][n+1] */
int **LCS = new int*[m+1];
for (int i = 0; i < m + 1; i++)
{
LCS[i] = new int[n+1];
}
/* 分別對LCS[i][0], LCS[0][j]賦值爲0 */
for (int i = 0; i < m+1; i++)
{
LCS[i][0] = 0;
}
for (int j = 0; j < n+1; j++)
{
LCS[0][j] = 0;
}
/* 分別遍歷兩個字符串,並更新LCS[i][j] */
for (int i = 1; i < m+1; i++)
{
for (int j = 1; j < n+1; j++)
{
if (pStr1[i-1] == pStr2[j-1])
{
LCS[i][j] = LCS[i-1][j-1] + 1;
}
else
{
LCS[i][j] = max(LCS[i-1][j], LCS[i][j-1]);
}
}
}
/* 獲取最長公共子序列 */
int longestCommonSequence = LCS[m][n];
/* 刪除動態空間 */
for (int i = 0; i < m + 1; i++)
{
delete [] LCS[i];
LCS[i] = NULL;
}
delete []LCS;
LCS = NULL;
/* 返回最長公共子序列 */
return longestCommonSequence;
}
void Test(const char *testName, const char *pStr1, const char *pStr2, int expectedLongestCommonSequence)
{
cout << testName << " : ";
if (GetLongestCommonSequence(pStr1, pStr2) == expectedLongestCommonSequence)
{
cout << "Passed." << endl;
}
else
{
cout << "Failed." << endl;
}
}
int main()
{
Test("Test1", "ABCBDAB", "BDCABA", 4);
Test("Test2", "A", "A", 1);
Test("Test3", "AB", "BC", 1);
}
(3) 求兩個字符串的編輯距離
問題:輸入兩個字符串,求它們的最短編輯距離。
我們定義了一套操作方法來把兩個不同的字符串變得相同,具體的操作方法是:
1. 修改一個字符(如把“a”替換爲“b”)
2. 增加一個字符(如把“abdd”變成“aebdd”)
3. 刪除一個字符(如把“travelling”變成“traveling”)
我們每執行上述一個步驟,則它們之間的編輯距離加1.
我們同樣定義一個二維數組,C[i][j]表示字符串Xi和字符串Yi的最短編輯距離。
C[i][j] = min{C[i-1][j] + 1, C [i][j-1] + 1,C [i-1][j-1]+ 1(xi != yj), C[i-1][j-1](xi = yj)}。
實現代碼如下:
#include <cstring>
#include <iostream>
using namespace std;
int min(int a, int b, int c)
{
int min = a;
if (min > b)
{
min = b;
}
if (min > c)
{
min = c;
}
return min;
}
int GetLeastestEditDistance(const char *pStr1, const char *pStr2)
{
if (pStr1 == NULL || pStr2 == NULL)
{
return -1;
}
int m = strlen(pStr1);
int n = strlen(pStr2);
/* 申請動態空間LED[m+1][n+1] */
int **LED = new int *[m+1];
for (int i = 0; i < m+1; i++)
{
LED[i] = new int[n+1];
}
/* 賦值LED[i][0] = i, LED[0][j] = j */
for (int i = 0; i < m+1; i++)
{
LED[i][0] = i;
}
for (int j = 0; j < n+1; j++)
{
LED[0][j] = j;
}
/* 計算LED[i][j] */
for (int i = 1; i < m+1; i++)
{
for (int j = 1; j < n+1; j++)
{
if (pStr1[i-1] == pStr2[j-1])
{
LED[i][j] = min(LED[i-1][j-1], LED[i-1][j] + 1, LED[i][j-1] + 1);
}
else
{
LED[i][j] = min(LED[i-1][j-1]+1, LED[i-1][j] + 1, LED[i][j-1] + 1);
}
}
}
/* 獲得最小的編輯距離 */
int leastestEditDistance = LED[m][n];
/* 釋放動態空間 */
for (int i = 0; i < m+1; i++)
{
delete [] LED[i];
LED[i] = NULL;
}
delete []LED;
LED = NULL;
/* 返回最小編輯距離 */
return leastestEditDistance;
}
void Test(const char *testName, const char *pStr1, const char *pStr2, int expectedLeastestEditDistance)
{
cout << testName << " : ";
if (GetLeastestEditDistance(pStr1, pStr2) == expectedLeastestEditDistance)
{
cout << "Passed." << endl;
}
else
{
cout << "Failed." << endl;
}
}
int main()
{
Test("Test1", "a", "b", 1);
Test("Test2", "abdd", "aebdd", 1);
Test("Test3", "travelling", "traveling", 1);
Test("Test4", "abcd", "abcd", 0);
Test("Test5", NULL, NULL, -1);
}