Common Subsequence

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, x ij = 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 题解出处:http://blog.csdn.net/a_eagle/article/details/7213236

题目大意:给出两个字符串,求两个字符串的最长公共字串。

思路:慢慢重心开始有贪心转向动态规划了,这题就是简单的动态规划题。以题目的第一组测试数据为例。abcfbc abfcab。

辅助空间变化示意图

可以看出:

F[i][j]=F[i-1][j-1]+1;(a[i]==b[j])

F[i][j]=max(F[i-1][j],F[i][j-1])(a[i]!=b[j]);

n由于F(i,j)只和F(i-1,j-1), F(i-1,j)和F(i,j-1)有关, 而在计算F(i,j)时, 只要选择一个合适的顺序, 就可以保证这三项都已经计算出来了, 这样就可以计算出F(i,j). 这样一直推到f(len(a),len(b))就得到所要求的解了.
 
ps:本题求不连续LCS,注释部分为连续LCS。
#include<stdio.h>
#include<string.h> int f[][];
char s1[],s2[]; int max(int x,int y)
{
return x>y?x:y;
} int main()
{
int n,i,j;
while(~scanf("%s %s",s1,s2)){
memset(f,,sizeof(f));
//int maxx=0;
for(i=;i<=strlen(s1);i++){
for(j=;j<=strlen(s2);j++){
if(s1[i-]==s2[j-]){
f[i][j]=f[i-][j-]+;
//if(f[i][j]>maxx) maxx=f[i][j];
}
//不加else
else f[i][j]=max(f[i-][j],f[i][j-]);
}
}
//printf("%d\n",maxx);
printf("%d\n",f[strlen(s1)][strlen(s2)]);
}
return ;
}

POJ - 1458 Common Subsequence DP最长公共子序列(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. POJ1458 Common Subsequence —— DP 最长公共子序列(LCS)

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

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

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

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

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

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

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

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

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

  8. POJ 1159 Palindrome(区间DP/最长公共子序列+滚动数组)

    Palindrome Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 56150   Accepted: 19398 Desc ...

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

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

随机推荐

  1. 关于maven的profile

    1 什么是profile profile本质上就是不同的环境对应不同的配置. 这样的好处是,在命令行中指定具体的profile的时候,会有自己独特的参数或者独特的配置来为不同的环境生成不同的目标代码. ...

  2. Dominant Resource Fairness: Fair Allocation of Multiple Resource Types

    Dominant Resource Fairness: Fair Allocation of Multiple Resource Types

  3. ZookeeperclientAPI之创建会话(六)

    Zookeeper对外提供了一套Java的clientAPI. 本篇博客主要讲一下创建会话. 创建项目 首选,创建一个基于maven管理的简单javaproject.在pom文件里引入zookeepe ...

  4. php 生成bing词典能导入的xml(有道词典->bing词典)

    编程以来一直用网易有道词典查单词.翻译:最近一直在看英文方面的资料,于是越来越对有道词典(划词.广告,本来想转灵格斯的,但灵格斯没有android版)不满意,一番试用后决定转bing词典,于是想把有道 ...

  5. java集合讲解干货集

    文章都来自网络,收集后便于查阅. 1.Java 集合系列01之 总体框架 2.Java 集合系列02之 Collection架构 3.Java 集合系列03之 ArrayList详细介绍(源码解析)和 ...

  6. JAVA各版本更新特性1-8

    JAVA各版本更新特性1-8 原文地址 Java Versions, Features and History This article gives you a highlight of import ...

  7. Perl 日期时间函数(date time)

    use Time::HiRes qw(time);use POSIX qw(strftime); my $t = time;my $date = strftime "%Y%m%d %H:%M ...

  8. 单页导航菜单视觉设计HTML模板

    单页导航菜单视觉设计HTML模板,视觉,企业,html,单页,单页导航菜单视觉设计HTML模板是一款磨砂背景的大气时尚HTML设计网页模板 http://www.huiyi8.com/moban/

  9. 通过在classpath自动扫描方式把组件纳入spring容器中管理。

    前面的例子我们都是使用xml的bean定义来配置组件,如果组件过多很臃肿.spring2.5引入了组件自动扫描机制,在指定目录下查找标注了@Component.@Service.@Controller ...

  10. Struts2与ServletAPI解耦

    什么是与Servlet API解耦? 为了避免与servlet API耦合在一起,方便Action做单元测试, Struts2对HttpServletRequest,HttpSession,和Serv ...