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. 【Howie玩docker】-命令行只显示-bash-4.1#

    灵雀云上面用docker建了个centOS的实例,首个免费,正好当云主机来玩. 但是,打开有个问题,命令行不显示当前用户和路径. 只显示: -bash-4.1# 简单,配置文件不全而已. 下面对其重新 ...

  2. 服务器是R710常见错误汇总:

    报错: E1422 CPU 1 machine check error . power cycle AC 解决方案: 系统 BIOS 已报告机器检查错误.请断开系统的交流电源 10 秒,然后重新启动系 ...

  3. Qt双缓冲机制:实现一个简单的绘图工具(纯代码实现)

    http://blog.csdn.net/rl529014/article/details/51658350

  4. QReadWriteLock上锁容忍的等待时间是多久?

    非递归上锁情况下,同一个锁,已经被锁过一次,那么另外一个线程尝试上锁,会被挡在外面,但是会被挡多久呢?挡一分钟会被崩溃,还十分钟,还是永久等待而不崩溃(希望如此)?还是会立刻崩溃?因为不可以重复上锁. ...

  5. 在两个Android设备间通过UDP传输目录内文件

    这两天下了一个使用UDP传输目录内文件的程序,发出来给大家一起看看,共同进步.有问题请指教. 由于udp丢包比较厉害,因此使用了自定义的内部协议,进行双方的确认. 程序跑起来后,看网络状况,有时候会一 ...

  6. Android 蓝牙开发(整理大全)

    Android蓝牙开发 鉴于国内Android蓝牙开发的例子很少,以及蓝牙开发也比较少用到,所以找的资料不是很全. (一): 由于Android蓝牙的通信都需要用到UUID,如果由手机发起搜索,当搜索 ...

  7. HDU 5741 Helter Skelter(构造法)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5741 [题目大意] 一个01相间的串,以0开头,给出的序列每个数字表示连续的0的个数或者1的个数, ...

  8. Apache Maven-AntRun-Plugin 官网 bug

    Maven AntRun Plugin 今天在 Apache maven-antrun-plugin 官网查找资料时,竟然发现了一个bug!! 在此记录下,以免在同一个地方摔倒两次! 想起一句话,尽信 ...

  9. Android-自己定义显示价格的PriceView

    转载请标明出处:http://blog.csdn.net/goldenfish1919/article/details/44418883 先看一下我们要做的效果: 价格分成了3部分.前面是一个¥,中间 ...

  10. css3选择器的比较(二) -- 包含字符串

    二.  包含“字符串” 两种用法的区别是: a. “~=”,需要用空格分割, b. "*=",不需要任何分隔符 1. 资料 a) b)  2. html代码 <div tit ...