hdu1159 最长公共子序列
Common Subsequence
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
http://blog.csdn.net/a_eagle/article/details/7213236
dp思想,不断更新当前的最长值。
AC代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std; int dp[][];
char a[],b[]; int main()
{
while(cin>>a>>b)
{
int len1=strlen(a);
int len2=strlen(b);
memset(dp,,sizeof(dp));
for(int i=; i<=len1; i++)
for(int j=; j<=len2; j++)
if(a[i-]==b[j-])
dp[i][j]=dp[i-][j-]+;
else
dp[i][j]=max(dp[i-][j],dp[i][j-]);
cout<<dp[len1][len2]<<endl;
}
return ;
}
hdu1159 最长公共子序列的更多相关文章
- hdu1503 最长公共子序列变形
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1503 题意:给出两个字符串 要求输出包含两个字符串的所有字母的最短序列.注意输出的顺序不能 ...
- 动态规划---最长公共子序列 hdu1159
hdu1159 题目要求两个字符串最长公共子序列, 状态转换方程 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] ...
- LCS最长公共子序列HDU1159
最近一直在学习算法,基本上都是在学习动态规划以及字符串.当然,两者交集最经典之一则是LCS问题. 首先LCS的问题基本上就是在字符串a,b之间找到最长的公共子序列,比如 YAOLONGBLOG 和 Y ...
- ACMDP之最长公共子序列长度—HDU1159
Common Subsequence Problem Description A subsequence of a given sequence is the given sequence with ...
- 【水:最长公共子序列】【HDU1159】【Common Subsequence】
Common Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- hdu1159 dp(最长公共子序列)
题意:给两个字符串,求这两个字符串的最长公共子序列的长度 因为之前集训的时候做过,所以现在即使会做也并不是什么稀奇的事,依旧为了自己的浅薄感到羞愧啊``` 解法就是通过两个字符串的每个字符互相比较,根 ...
- 最长公共子序列-Hdu1159
Common Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- 用python实现最长公共子序列算法(找到所有最长公共子串)
软件安全的一个小实验,正好复习一下LCS的写法. 实现LCS的算法和算法导论上的方式基本一致,都是先建好两个表,一个存储在(i,j)处当前最长公共子序列长度,另一个存储在(i,j)处的回溯方向. 相对 ...
- 动态规划之最长公共子序列(LCS)
转自:http://segmentfault.com/blog/exploring/ LCS 问题描述 定义: 一个数列 S,如果分别是两个或多个已知数列的子序列,且是所有符合此条件序列中最长的,则 ...
随机推荐
- 【leetcode】Combinations (middle)
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...
- 【leetcode】Remove Element (easy)
Given an array and a value, remove all instances of that value in place and return the new length. T ...
- va_list使用
http://www.programfan.com/blog/article.asp?id=41937
- HDU 5894 hannnnah_j’s Biological Test (组合数学) -2016 ICPC沈阳赛区网络赛
题目链接 #include <map> #include <queue> #include <math.h> #include <stdio.h> #i ...
- Java常用工具类题库
一. 填空题 在Java中每个Java基本类型在java.lang包中都在一个相应的包装类,把基本类型数据转换为对象,其中包装类Integer是___Number__的直接子类. 包装类Inte ...
- [网络流24题] 太空飞行计划(cogs 727)
[问题描述] W 教授正在为国家航天中心计划一系列的太空飞行.每次太空飞行可进行一系列商业性实验而获取利润.现已确定了一个可供选择的实验集合E={E1,E2,-,Em},和进行这些实验需要使用的全部仪 ...
- spring整合httpclient
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://w ...
- nyoj744(位运算)
题目:http://acm.nyist.net/JudgeOnline/problem.php?pid=744 思路:a^b可以得到a~b间任意两个数异或运算的长度的最大值,设为n,答案为:pow(2 ...
- codevs 2851 菜菜买气球
dp加二分法 链接:http://codevs.cn/problem/2851/ #include<iostream> #include<vector> #include< ...
- route netstat -rn
-n :不要使用通讯协定或主机名称,直接使用ip或port number; -ee:使用更详细的资讯来显示 [root@NB data]# route -nee Kernel IP routing t ...