POJ 1458 Common Subsequence(LCS最长公共子序列)解题报告

题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87730#problem/F

题目:

Common Subsequence
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 43388   Accepted: 17613

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.

Input

The program input is from the std input. Each data set in the input contains two strings representing the given sequences. The sequences are separated by any number of white spaces. The input data are correct.

Output

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 题目大意:
给出两个字符串,求两字符串的最长公共子序列。 分析:
很明显用LCS,时间复杂度O(nm),其中n,m是序列A和B的长度。当s1[i-1]==s2[k-1]时,d(i,k)=d(i-1,k-1)+1,否则,
d(i,k)=max{d(i-1,k),d(i,k-1)}。 代码:
 #include<cstdio>
#include<iostream>
#include<cstring>
using namespace std; const int maxn=; int dp[maxn][maxn]; int max(int a,int b)
{
return a>b?a:b;
} int main()
{
char s1[maxn],s2[maxn];
while(scanf("%s%s",s1,s2)!=EOF)
{
int m=strlen(s1);
int n=strlen(s2);
memset(dp,,sizeof(dp));
for(int i=;i<=m;i++)
{
for(int k=;k<=n;k++)
{
if(s1[i-]==s2[k-]) //s1和s2相等,dp+1
dp[i][k]=dp[i-][k-]+;
else //s1和s2不相等时看下一个
dp[i][k]=max(dp[i-][k],dp[i][k-]);
}
}
printf("%d\n",dp[m][n]);
}
return ;
}
												

POJ 1458 Common Subsequence(LCS最长公共子序列)的更多相关文章

  1. POJ 1458 Common Subsequence 【最长公共子序列】

    解题思路:先注意到序列和串的区别,序列不需要连续,而串是需要连续的,先由样例abcfbc         abfcab画一个表格分析,用dp[i][j]储存当比较到s1[i],s2[j]时最长公共子序 ...

  2. POJ 1458 Common Subsequence(最长公共子序列)

    题目链接Time Limit: 1000MS Memory Limit: 10000K Total Submissions: Accepted: Description A subsequence o ...

  3. POJ - 1458 Common Subsequence DP最长公共子序列(LCS)

    Common Subsequence A subsequence of a given sequence is the given sequence with some elements (possi ...

  4. hdu 1159 Common Subsequence(LCS最长公共子序列)

    Common Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  5. 题解报告:hdu 1159 Common Subsequence(最长公共子序列LCS)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159 Problem Description 给定序列的子序列是给定的序列,其中有一些元素(可能没有) ...

  6. POJ1458 Common Subsequence —— DP 最长公共子序列(LCS)

    题目链接:http://poj.org/problem?id=1458 Common Subsequence Time Limit: 1000MS   Memory Limit: 10000K Tot ...

  7. hdu 1159 Common Subsequence(最长公共子序列)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159 Common Subsequence Time Limit: 2000/1000 MS (Jav ...

  8. hdu 1159 Common Subsequence (最长公共子序列 +代码)

    Problem Description A subsequence of a given sequence is the given sequence with some elements (poss ...

  9. HDU 1159 Common Subsequence 【最长公共子序列】模板题

    题目链接:https://vjudge.net/contest/124428#problem/A 题目大意:给出两个字符串,求其最长公共子序列的长度. 最长公共子序列算法详解:https://blog ...

  10. hdu 1159 Common Subsequence(最长公共子序列,DP)

    题意: 两个字符串,判断最长公共子序列的长度. 思路: 直接看代码,,注意边界处理 代码: char s1[505], s2[505]; int dp[505][505]; int main(){ w ...

随机推荐

  1. JAVA并发,CountDownLatch使用

    该文章转自:http://www.itzhai.com/the-introduction-and-use-of-a-countdownlatch.html CountDownLatch 1.类介绍 一 ...

  2. java axis web service

    编写 java调用web service的客户端比较简单,其中webservice为上一篇gsoap创建的server. package clientTest; import java.rmi.Rem ...

  3. DateTimePicker——开源的Android日历类库

    Github托管地址:https://github.com/flavienlaurent/datetimepicker

  4. ComboBox控件绑定数据源

    最近在研究机房收费系统的组合查询的方法时,看到了ComboBox控件可以进行数据绑定,我觉得这个功能真的很不错,可以给我省去很多的麻烦. 下面是我组合查询窗体界面 一.数据转换方法 现在我们开看一下我 ...

  5. BZOJ 1177 [Apio2009]Oil(递推)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1177 [题目大意] 给出一个矩阵,从中选出3个k*k且不相交的矩阵,使得其总和最大 [ ...

  6. Java知识点复习

    总结下java的知识点 final 关键字-方法:不能被子类重写(override)-变量:不能被修改-类:不可以被继承,派生子类 finally 关键字与try/catch语句配合使用,即使有异常抛 ...

  7. HEVC与VP9编码效率对比

    HEVC(High EfficiencyVideo Coding,高效率视频编码)是一种视频压缩标准,H.264/MPEG-4 AVC的继任者.目前正在由ISO/IEC MPEG和ITU-T VCEG ...

  8. word排版论文小结

    毕业论文如何用WORD排版 本人折腾了一晚上看别人的百度经验,做个总结,方便后人看懂,其实特别简单 用WORD自动生成页码 第一种情况:从第一页或者从第二页开始设置页码 这种情况只要“插入-> ...

  9. ie6兼容性,还需要测试么?迷茫。。。

    最近公司网站在谷歌,火狐上测试都没有问题,但是在ietest,ie6上出现兼容问题 ,由于ietest好几次打开ie6都报错(尝试卸载重新安装几次无果),下载virtualbox安装自带ie6的xp系 ...

  10. 现在输入 n 个数字, 以逗号, 分开; 然后可选择升或者 降序排序;

    /* 现在输入 n 个数字, 以逗号, 分开: 然后可选择升或者 降序排序: */ import java.util.*; public class bycomma{ public static St ...