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. Python GUI开发环境的搭建

    原文:Python GUI开发环境的搭建 最近对Python的开发又来了兴趣,对于Python的开发一直停留在一个表面层的认识,玩的部分比较大. Python的入手简单,语法让人爱不释手,在网络通信方 ...

  2. 去掉Flex4生成的SWF加载时的进度条

    原文 http://www.cnblogs.com/modou/articles/1955507.html 方法一: <?xml version="1.0" encoding ...

  3. file_get_contents 超时设置

    <?php $ctx = stream_context_create( array( 'http' => array( 'timeout' => 1 //设置一个超时时间,单位为秒 ...

  4. 在python中编写socket服务端模块(二):使用poll或epoll

    在linux上编写socket服务端程序一般可以用select.poll.epoll三种方式,本文主要介绍使用poll和epoll编写socket服务端模块. 使用poll方式的服务器端程序代码: i ...

  5. CCNA实验(8) -- PPP & HDLC

    HDLC帧格式与以太帧格式有很大差别,HDLC帧没有源MAC和目的MAC地址.HDLC不能提供验证,缺少对链路保护.Cisco设备与Cisco设备连接,可用HDLC封装.Cisco设备与非Cisco设 ...

  6. HDU 5765 Bonds(状压DP)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5765 [题目大意] 给出一张图,求每条边在所有边割集中出现的次数. [题解] 利用状压DP,计算不 ...

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

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

  8. [置顶] jsp中c标签的使用

    jsp中c标签的使用 核心标签库 它是JSTL中的核心库,为日常任务提供通用支持,如显示和设置变量.重复使用一组项目.测试条件和其他操作(如导入和重定向Web内容).Core标签按功能可分为4种类型: ...

  9. canvas绘制弹跳小球

    <!doctype html> <html> <head> <meta charset="UTF-8"> <title> ...

  10. Bootstrap第一天

    1.代码引入: 第一步:在html5文档 <meta name="viewport" content="width=device-width, initial-sc ...