Common Subsequence

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 49137    Accepted Submission(s): 22632

Problem 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. 
The program input is from a text file. Each data set in the file contains two strings representing the given sequences. The sequences are separated by any number of white spaces. The input data are correct. 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
 

题意:求两个字符串的最长公共子序列,一个经典问题

当a[i]==b[j]时  dp[i][j]=dp[i-1][j-1]+1

当a[i]!=b[j]时 dp[i][j]=max(dp[i][j-1],dp[i-1][j])

 #include<bits/stdc++.h>
using namespace std;
char a[],b[];
int dp[][];
int main() { while(~scanf("%s %s",a+,b+)) {
memset(dp,,sizeof(dp)); int maxx=-;
int alen=strlen(a+);
int blen=strlen(b+);
for(int i=; i<=alen; i++) {
for(int j=; j<=blen; j++) {
if(a[i]==b[j])dp[i][j]=dp[i-][j-]+;
else dp[i][j]=max(dp[i-][j],dp[i][j-]);
//maxx=max(maxx,dp[i][j]);
}
}
printf("%d\n",dp[alen][blen]);
memset(a,'\0',sizeof(a));
memset(b,'\0',sizeof(b));
}
return ;
}

hdu1159Common Subsequence(动态规划)的更多相关文章

  1. hdu1159Common Subsequence——动态规划(最长公共子序列(LCS))

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

  2. HDU 1159 Common Subsequence 动态规划

    2017-08-06 15:41:04 writer:pprp 刚开始学dp,集训的讲的很难,但是还是得自己看,从简单到难,慢慢来(如果哪里有错误欢迎各位大佬指正) 题意如下: 给两个字符串,找到其中 ...

  3. POJ 2127 Greatest Common Increasing Subsequence -- 动态规划

    题目地址:http://poj.org/problem?id=2127 Description You are given two sequences of integer numbers. Writ ...

  4. HDOJ 1423 Greatest Common Increasing Subsequence -- 动态规划

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1423 Problem Description This is a problem from ZOJ 2 ...

  5. HDU1159-Common Subsequence

    描述: A subsequence of a given sequence is the given sequence with some elements (possible none) left ...

  6. 算法:Common Subsequence(动态规划 Java 最长子序列)

    Description A subsequence of a given sequence is the given sequence with some elements (possible non ...

  7. HDU 1423 Greatest Common Increasing Subsequence ——动态规划

    好久以前的坑了. 最长公共上升子序列. 没什么好说的,自己太菜了 #include <map> #include <cmath> #include <queue> ...

  8. 杭电ACM分类

    杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze ...

  9. 转载:hdu 题目分类 (侵删)

    转载:from http://blog.csdn.net/qq_28236309/article/details/47818349 基础题:1000.1001.1004.1005.1008.1012. ...

随机推荐

  1. 关于PHP数组你应该知道的事情

    (1).PHP数组的遍历顺序 先举个栗子: <?php $arr['a'] = '123'; $arr['b'] = '456'; $arr['c'] = '789'; foreach($a a ...

  2. 微信小程序开发工具快捷键

    格式调整 //保存文件 Ctrl+S //代码行缩进 Ctrl+[, Ctrl+] //折叠打开代码块 Ctrl+Shift+[, Ctrl+Shift+] //复制粘贴,如果没有选中任何文字则复制粘 ...

  3. js秒换成天时分

    js秒换成天时分 function timeStamp( second_time ){ var time = parseInt(second_time) + "秒"; if( pa ...

  4. CSS兼容性问题总结及解决方法

    css兼容问题 兼容问题 1.文字本身的大小不兼容.同样是font-size:14px的宋体文字,在不同浏览器下占的空间是不一样的,ie下实际占高16px,下留白3px,ff下实际占高17px,上留白 ...

  5. maven 打包时动态替换properties资源文件中的配置值

    pom build节点下面添加resource配置: <resources> <resource> <directory>src/main/resources/&l ...

  6. iOS中的应用启动原理

    iOS中的应用启动原理 来源: http://m.blog.csdn.net/article/details?id=50530090 http://m.warting.com/program/2016 ...

  7. 【SQLSERVER学习笔记】细节记录

    SQLSERVER 查询时,WHERE中使用<>时,不会把NULL值查出来. SQLSERVER子查询中不能使用 ORDER BY. SQLSERVER 使用DISTINCT时,必须把OR ...

  8. 通用输入输出端口 - GPIO

    一.概述 GPlO ( General Purpose I/0 Ports )意思为通用输入/输出端口, 通俗地说, 就是一些引脚.在芯片手册中I/O端口一般是分组的,比如有的芯片分为 A-J 共 9 ...

  9. java中匿名内部类总结

    在java的世界里,提供了匿名内部类语法糖,用于帮助大家简化代码,本文简要从接口,抽象类以及常规类以代码的形式描述其常用模式. 1. 接口模式 public interface IWriter { v ...

  10. mysql数据库迁移到oracle数据库后 如何删除相同的数据

    mysql数据库迁移到oracle数据库后 如何删除相同的数据 首先搞清楚有多少数据是重复的 select pid from product group by pid having count(pid ...