Common Subsequence(DP)

/*http://acm.hdu.edu.cn/diy/diy_previewproblem.php?cid=19572&pid=1022
         Common Subsequence

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 0   Accepted Submission(s) : 0
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
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
4
2
0
Source
Southeastern Europe 2003
Close | Top
Hangzhou Dianzi Univ
解析:求最长公共子序列问题
常用DP,从后往前推if(s1[i-1]==s2[j-1])//若两数相同则加1
              f[i][j]=f[i-1][j-1]+1;
              else//若不同则返回前面过程
              f[i][j]=max(f[i-1][j],f[i][j-1]);
*/

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include <iostream>
using namespace std;
const int maxn=1000+10;
char s1[maxn],s2[maxn];
int f[maxn][maxn];
int inf=10000000;
int max(int a,int b)
{
    return a>b? a:b;
}
int main()
{
    int len1,len2;
    int i,j;
    while(scanf("%s",s1)!=EOF)
    {
        scanf("%s",s2);
         memset(f,0,sizeof(f));
         len1=strlen(s1);
         len2=strlen(s2);
         for(i=1;i<=len1;i++)
           for(j=1;j<=len2;j++)
           { if(s1[i-1]==s2[j-1])//若两数相同则加1
              f[i][j]=f[i-1][j-1]+1;
              else//若不同则返回前面过程
              f[i][j]=max(f[i-1][j],f[i][j-1]);

           }
           printf("%d\n",f[len1][len2]);
    }
    return 0;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 最长公共子序列(Longest Common Subsequence)指的是在两个序列中找到最长的公共子序列,这个公共子序列可以不连续,但是需要保持相对顺序不变。例如,对于序列ABCD和ACDFG,它们的最长公共子序列是ACD。 ### 回答2: 最长公共子序列(Longest Common Subsequence,简称LCS)是指在给定多个序列中,找到最长的一个子序列,该子序列同时出现在这些序列中,并且其他元素的相对顺序保持一致。 举个例子,假设有两个序列A和B,A为[1, 2, 3, 4, 5],B为[2, 4, 5, 6]。它们的一个最长公共子序列是[2, 4, 5],该子序列同时存在于A和B中。 求解LCS的问题可以用动态规划的方法来解决。我们可以构建一个二维数组dp,其中dp[i][j]表示序列A的前i个元素和序列B的前j个元素的LCS长度。那么dp[i][j]可以通过以下方式得到: 1. 如果A[i]等于B[j],则dp[i][j]等于dp[i-1][j-1] + 1; 2. 如果A[i]不等于B[j],则dp[i][j]等于max(dp[i-1][j], dp[i][j-1])。 通过填充整个dp数组,最终可以得到序列A和序列B的LCS长度。要找到具体的LCS序列,则可以通过反向遍历dp数组进行构建。 LCS问题在字符串处理、DNA序列匹配、版本控制等领域都有广泛的应用。其时间复杂度为O(m*n),其中m和n分别为序列A和序列B的长度。 ### 回答3: 最长公共子序列(Longest Common Subsequence)是一个经典的计算机科学问题。给定两个序列S和T,我们要找出它们之间最长的公共子序列。 子序列是从给定序列中按顺序选择几个元素而组成的序列。而公共子序列指的是同时是序列S和T的子序列的序列。 为了解决这个问题,可以使用动态规划的方法。我们可以定义一个二维数组dp,其中dp[i][j]表示序列S的前i个元素和序列T的前j个元素之间的最长公共子序列的长度。 接下来,我们可以使用以下递推关系来填充dp数组: 如果S[i]等于T[j],则dp[i][j] = dp[i-1][j-1] + 1; 如果S[i]不等于T[j],则dp[i][j] = max(dp[i-1][j], dp[i][j-1])。 最后,我们可以通过查看dp[S.length()][T.length()]来得到最长公共子序列的长度。 此外,我们也可以用回溯法来还原最长公共子序列本身。我们可以从dp[S.length()][T.length()]开始,如果S[i]等于T[j],则将S[i]添加到结果序列中,并向左上方移动,即i = i-1,j = j-1。如果S[i]不等于T[j],则根据dp数组的值选择向上(i = i-1)或向左(j = j-1)移动。 总之,最长公共子序列问题是一个经典的计算机科学问题,可以使用动态规划的方法解决。我们可以通过构建二维dp数组来计算最长公共子序列的长度,并可以使用回溯法来还原它本身。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值