HD1159 Common Subsequence 最长公共子序列(LCS)

Common Subsequence

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 23500    Accepted Submission(s): 10355


Problem Description
A subsequence of a given sequence is the given sequence with some elements (possible none) left out. Given a sequence X = another sequence Z = is a subsequence of X if there exists a strictly increasing sequence of indices of X such that for all j = 1,2,...,k, xij = zj. For example, Z = is a subsequence of X = 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
 
   
4 2 0
 

Source
Southeastern Europe 2003
 

Recommend
Ignatius   |   We have carefully selected several similar problems for you:   1087  1176  1003  1058  1069 


最长公共子序列(LCS)的问题,在《算法导论(第三版)》P222有详细说明,可得出以下结论:

c[i][j]表示一个字符串x的前i个字符,与另一个字符串y的前j个字符的最长公共子序列。【字符从1开始编号。】
i或j为0,有字符串为空,LCS长度自然为0;
当字符串x的第i个字符与y的第j个字符相同时,这个字符也在LCS中,故其长度等于x的前i-1个字符与y的前j-1个字符的LCS长度+1;
当字符串x的第i个字符与y的第j个字符不同时,则为c[i - 1][j]与c[i][j - 1])的最大值。
由于自己最近状态不好,字符要从1开始编号有点不适应,编程时写错了许多细节,故在程序中注释出来作为提醒。

Run ID

Submit Time

Judge Status

Pro.ID

Exe.Time

Exe.Memory

Code Len.

Language

Author

11439824

2014-08-14 17:13:56

Accepted

1159

31MS

2404K

948B

G++

hnshhslsh


#include
#include
#include
using namespace std;

const int MAXN = 1111;
char a[MAXN],b[MAXN];
int c[MAXN][MAXN];

int main() {
    while(~scanf("%s%s",a,b)) {
        int m = strlen(a);
        int n = strlen(b);
        for(int i = 0; i <= n; ++i)    ///从0到n或m,包括n,m
            c[i][0] = 0;
        for(int i = 0; i <= m; ++i)
            c[0][i] = 0;
        for(int i = 1; i <= n; ++i)    ///从1到n或m,包括n,m
            for(int j = 1; j <= m; ++j) {
                if(a[j - 1] == b[i - 1])    ///下标要-1
                    c[i][j] = c[i - 1][j - 1] + 1;    ///+1又忘了:(
                else
                    c[i][j] = max(c[i - 1][j],c[i][j - 1]);
            }
        printf("%d\n",c[n][m]);
    }
    return 0;
}

这个程序开了一个MAX × MAX 的数组,这不是必须的,

其实只要求长度的话,整个数组有用的部分只有一行+1个元素

下面这个程序经行了空间的优化

Run ID

Submit Time

Judge Status

Pro.ID

Exe.Time

Exe.Memory

Code Len.

Language

Author

11443691

2014-08-14 20:56:59

Accepted

1159

31MS

232K

768B

G++

hnshhslsh


#include
#include
#include
using namespace std;

const int MAXN = 1111;
char a[MAXN],b[MAXN];
int c[MAXN];

int main() {
    while(~scanf("%s%s",a,b)) {
        int m = strlen(a);
        int n = strlen(b);
        for(int i = 0; i <= m; ++i)
            c[i] = 0;
        for(int i = 1; i <= n; ++i) {
            int t = 0,ans;
	///t由于保存上一个空间中原来存放的元素,也就是原来二维数组中当前元素左上角的元素;ans用来暂时储存答案
            for(int j = 1; j <= m; ++j) {
                if(a[j - 1] == b[i - 1]) {
                    ans = t + 1;	///相当于 ans = c[i - 1][j - 1] + 1;
                    t = c[j];	///用t保存下一次运算的c[i - 1][j - 1]的值备用
                    c[j] = ans;
                } else {
                    t = c[j];
                    c[j] =  max(c[j - 1],c[j]);	///相当于c[i][j] = max(c[i - 1][j],c[i][j - 1]);
                }
            }
        }
        printf("%d\n",c[m]);
    }
    return 0;
}






你可能感兴趣的:(字符串,动态规划)