Common Subsequence
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 43132   Accepted: 17472

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 虽然还没有太理解这个算法,不过还是先贴上吧
#include<stdio.h>
#include<string.h>
#define MAX 1100
#define maxn(x,y)(x>y?x:y)
char s1[MAX],s2[MAX];
int dp[MAX][MAX];
int main()
{
int i,j,len1,len2;
memset(dp,0,sizeof(dp));
while(scanf("%s%s",s1,s2)!=EOF)
{
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])
dp[i][j]=dp[i-1][j-1]+1;
else
dp[i][j]=maxn(dp[i][j-1],dp[i-1][j]);
}
}
printf("%d\n",dp[len1][len2]);
}
return 0;
}

  

poj 1458 Common Subsequence【LCS】的更多相关文章

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

    POJ 1458 Common Subsequence(LCS最长公共子序列)解题报告 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?c ...

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

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

  3. hdoj 1159 Common Subsequence【LCS】【DP】

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

  4. POJ_1458 Common Subsequence 【LCS】

    一.题目 Common Subsequence 二.分析 比较基础的求最长升序子序列. $DP[i][j]$表示的是字符串$S1[1...i]$与$S2[1...j]$的最长公共子序列长度. 状态转移 ...

  5. poj 1458 Common Subsequence ——(LCS)

    虽然以前可能接触过最长公共子序列,但是正规的写应该还是第一次吧. 直接贴代码就好了吧: #include <stdio.h> #include <algorithm> #inc ...

  6. LCS POJ 1458 Common Subsequence

    题目传送门 题意:输出两字符串的最长公共子序列长度 分析:LCS(Longest Common Subsequence)裸题.状态转移方程:dp[i+1][j+1] = dp[i][j] + 1; ( ...

  7. HDOJ 1159 Common Subsequence【DP】

    HDOJ 1159 Common Subsequence[DP] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...

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

    POJ1458 Common Subsequence(最长公共子序列LCS) http://poj.org/problem?id=1458 题意: 给你两个字符串, 要你求出两个字符串的最长公共子序列 ...

  9. hdu 1159 Common Subsequence 【LCS 基础入门】

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=1159 http://acm.hust.edu.cn/vjudge/contest/view.action ...

随机推荐

  1. Gprinter Android SDK V1.0 使用说明

    佳博打印机代理商淘宝店https://shop107172033.taobao.com/index.htm?spm=2013.1.w5002-9520741823.2.Sqz8Pf 在此店购买的打印机 ...

  2. Java环境的安装与配置

    Java环境的安装与配置 环境:Java8,win10 推荐oracle官网oracle官网https://www.oracle.com/index.html下载JDK进行安装 选择自己需要的版本下载 ...

  3. sid超过8个字符处理步骤

    服务端配置如下: [oracle@p3 admin]$ cat listener.ora # listener.ora Network Configuration File: /home/oracle ...

  4. power desinger 学习笔记<八>

    转-PowerDesigner 把Comment复制到name中和把name复制到Comment 在使用PowerDesigner对数据库进行概念模型和物理模型设计时,一般在NAME或Comment中 ...

  5. Javascript实现Web颜色值转换

    最近一直忙碌于完成业务需求,好长时间没有写博客了.今天稍微有些时间,翻看了一下最近项目中的一些前端代码,看到Web颜色转换功能的时候,突然想到当我们在做一些颜色设置/编辑的需求时,经常会涉及到各种颜色 ...

  6. OC - 22.隐式动画

    简介 每个UI控件,默认自动创建一个图层(根图层),即每个UI控件对应于至少一个图层 每一个UIView内部都默认关联着一个CALayer,我们可用称这个Layer为Root Layer(根层)   ...

  7. iOS将产品进行多语言发布,开发

    多语言就是程序的国际化.在Xcode中要实现程序的国际化,只需要简单配置,并修改相应的字符串键值对即可. 应用程序的国际化主要包括三个方面:A.程序名称国际化:B.程序内容国际化:C.程序资源国际化 ...

  8. C# var

    VAR 是3.5新出的一个定义变量的类型其实也就是弱化类型的定义VAR可代替任何类型编译器会根据上下文来判断你到底是想用什么类型的 至于什么情况下用到VAR 我想就是你无法确定自己将用的是什么类型就可 ...

  9. npm的镜像替换成淘宝

    1.得到原本的镜像地址 npm get registry > https://registry.npmjs.org/ 设成淘宝的 npm config set registry http://r ...

  10. Tomcat 8熵池阻塞变慢详解(转)

    Tomcat 8熵池阻塞变慢详解 作者:chszs,转载需注明.博客主页:http://blog.csdn.net/chszs Tomcat 8启动很慢,且日志上无任何错误,在日志中查看到如下信息: ...