連接:
Common Subsequence
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 17621 Accepted Submission(s): 7401
Problem Description
A subsequence of a given sequence is the given sequence with some elements (possible none) left out. Given a sequence X = <x1, x2, ..., xm> another sequence Z = <z1, z2, ..., zk> is a subsequence of X if there exists a strictly increasing sequence <i1, i2, ..., ik> of indices of X such that for all j = 1,2,...,k, xij = zj. For example, Z = <a, b, f, c> is a subsequence of X = <a, b, c, f, b, c> with index sequence <1, 2, 4, 6>. Given two sequences X and Y the problem is to find the length of the maximum-length common subsequence of X and Y.
The program input is from a text file. Each data set in the file contains two strings representing the given sequences. The sequences are separated by any number of white spaces. The input data are correct. For each set of data the program prints on the standard output the length of the maximum-length common subsequence from the beginning of a separate line.
Sample Input
abcfbc abfcab
programming contest
abcd mnp
Sample Output
Source
Recommend
Ignatius
題意:
求最長公共上升子序列
就是第一個序列和第二個序列中有幾個相同的。
子序列:
若是有一個序列 <A1, A2, A3,... An> 還有個序列 <Ak1, A k2, ... A kn> 知足 k1 < k2 < k3,
那麼 第二個序列是第一個序列的子序列
最長公共子序列:
就是從幾個序列【通常是兩個了】中找出同樣的最長的子序列
分析:
用 dp[i][j] 記錄序列 A 中從 0 到 i-1 和序列 B 中從 0 到 j-1 的最長公共子序列長度
O(n^n)寫法:
先初始化 dp 爲 0
當 A 序列遍歷到第 i-1 個,序列 B 遍歷到第 j-1 個
1) 若是此時 A[i-1] == B[j-1] , 那麼可想而知 dp[i][j] = dp[i-1][j-1] +1
2) 若是此時 A[i-1] != B[j-1], 那麼 dp[i][j] = max( dp[i-1][j], dp[i][j-1] )
總之至關於記憶化搜索的了 dp[i][j] 從左到右從上到下, 當確立了當前字符是否相等時
那麼 dp[i][j] 就由它的左上角 ( dp[i-1][j-1] )、上面的 ( dp[i-1][j] )、前面的 ( dp[i][j-1] ) 肯定
/**
求長度爲 len1 的序列 A 和長度爲 len2 的序列 B 的LCS
注意:序列下標從 0 開始
*/
void LCS(int len1,int len2)
{
for(int i = 1; i <= len1; i++)
{
for(int j = 1; j <= len2; j++)
{
if(s1[i-1] == s2[j-1]) dp[i][j] = dp[i-1][j-1]+1;
else
{
int m1 = dp[i-1][j];
int m2 = dp[i][j-1];
dp[i][j] = max(m1, m2);
}
}
}
}
比完賽了學妹提到的,將 dp[maxn][maxn]優化到 dp[2][maxn]
其實也是滾動數組的概念了:
由上面的分析咱們發現這一點:當前的 dp[i][j] 只是與以它爲右下角的四個 dp 有關
dp[i-1][j-1] dp[i-1][j]
dp[i][j-1] dp[i][j]
而咱們並非要求出每一段的 LCS 而只是求出最終的長度的 LCS,那麼每次對 i %2 就能夠解決問題
這樣就大大優化了內存
void LCS(int len1,int len2)
{
for(int i = 1; i <= len1; i++)
{
for(int j = 1; j <= len2; j++)
{
if(s1[i-1] == s2[j-1]) dp[i%2][j] = dp[(i-1)%2][j-1]+1;
else
{
int m1 = dp[(i-1)%2][j];
int m2 = dp[i%2][j-1];
dp[i%2][j] = max(m1, m2);
}
}
}
}
code:
普通:
A |
Accepted |
1244 KB |
31 ms |
C++ |
749 B |
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxn = 500+10;
int dp[maxn][maxn];
char s1[maxn], s2[maxn];
/**
求長度爲 len1 的序列 A 和長度爲 len2 的序列 B 的LCS
注意:序列下標從 0 開始
*/
void LCS(int len1,int len2)
{
for(int i = 1; i <= len1; i++)
{
for(int j = 1; j <= len2; j++)
{
if(s1[i-1] == s2[j-1]) dp[i][j] = dp[i-1][j-1]+1;
else
{
int m1 = dp[i-1][j];
int m2 = dp[i][j-1];
dp[i][j] = max(m1, m2);
}
}
}
}
int main()
{
while(scanf("%s%s", s1,s2) != EOF)
{
int len1 = strlen(s1);
int len2 = strlen(s2);
memset(dp,0,sizeof(dp));
LCS(len1, len2);
printf("%d\n", dp[len1][len2]);
}
}
滾動數組:
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxn = 500+10;
int dp[2][maxn];
char s1[maxn], s2[maxn];
void LCS(int len1,int len2)
{
for(int i = 1; i <= len1; i++)
{
for(int j = 1; j <= len2; j++)
{
if(s1[i-1] == s2[j-1]) dp[i%2][j] = dp[(i-1)%2][j-1]+1;
else
{
int m1 = dp[(i-1)%2][j];
int m2 = dp[i%2][j-1];
dp[i%2][j] = max(m1, m2);
}
}
}
}
int main()
{
while(scanf("%s%s", s1,s2) != EOF)
{
int len1 = strlen(s1);
int len2 = strlen(s2);
memset(dp,0,sizeof(dp));
LCS(len1, len2);
printf("%d\n", dp[len1%2][len2]);
}
}