(hdu step 3.2.2)Common Subsequence(简单dp:求最长公共子序列的长度)
来源:互联网 发布:软装饰品知乎 编辑:程序博客网 时间:2024/05/21 05:23
在写题解之前给自己打一下广告哈~。。抱歉了,希望大家多多支持我在CSDN的视频课程,地址如下:
http://edu.csdn.net/course/detail/209
题目:
Common Subsequence
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 976 Accepted Submission(s): 538Problem 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.
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 abfcabprogramming contest abcd mnp
Sample Output
420
Source
Southeastern Europe 2003
Recommend
Ignatius
题目分析:
简单DP。求最长公共子序列的长度(Longest Common Sequence,LCS)。其实使用DP的思想在求LCS的时候,使用的还是暴力的思想。遍历两个字符串中的每一个字符,并且判断它们是否相同。这道题需要明白以下两点:
1)状态转移方程
2)使用回溯法求LCS的过程
对于动态规划题的一些整理可以参考一下:http://blog.csdn.net/hjd_love_zzt/article/details/26979313
代码如下:
/* * b.cpp * * Created on: 2015年2月6日 * Author: Administrator */#include <iostream>#include <cstdio>#include <cmath>using namespace std;const int maxn = 1005;char a[maxn];char b[maxn];int c[maxn][maxn];int get(int i,int j){if(i >= 0 && j >= 0){return c[i][j];}return 0;}int main(){while(scanf("%s %s",&a,&b)!=EOF){int n = strlen(a);int m = strlen(b);memset(c,0,sizeof(c));//c[i][j]表示的是a序列的前i个字符与b序列的前j个字符所形成的最长公共子序列的最大长度int i;int j;for(i = 0 ; i < n ; ++i){for(j = 0 ; j < m ; ++j){if(a[i] == b[j]){//判断a序列中的第i个字符和b序列中的第j个字符是否相同/** * 如果相同,则a序列中的前i个字符和b序列中的前j个字符的最长公共子序列的长度== * a序列中的前i-1个字符和b序列中的前j-1个字符的最长公共子序列的长度 + 1(加1是因为最后一个字符相同,所以最长公共子序列的长度+1) */c[i][j] = get(i-1,j-1) + 1;}else{//如果a序列的第i个字符和b序列的第j个字符不相同/** * a序列的前i个字符和b序列的前j个字符所形成的最长公共子序列的长度是 * 1)前i-1个字符与前j个字符所形成的最长公共子序列的长度 * 2)前i个字符与前j-1个字符所形成的最长公共子序列的长度 * 中的较大值 */c[i][j] = max(get(i-1,j),get(i,j-1));}}}printf("%d\n",c[n-1][m-1]);//这里为什么输出的索引是n-1和m-1呢。因为索引是从0开始的.n-1已经是最后一个字符的索引}return 0;}
2 1
- (hdu step 3.2.2)Common Subsequence(简单dp:求最长公共子序列的长度)
- HDU 1159 Common Subsequence (求两个串的最长公共子序列 dp)
- hdu 1159 Common Subsequence(DP最长公共子序列)
- 最长公共子序列DP Common Subsequence HDU 1159
- HDU 1159 Common Subsequence--DP--(最长公共子序列)
- 最长公共子序列(dp) & hdu 1159 Common Subsequence
- hdu 1159 Common Subsequence(最长公共子序列 DP)
- HDU 1159 Common Subsequence(DP最长公共子序列)
- HDU 1159 Common Subsequence(最长公共子序列)dp
- 【HDU】1159 Common Subsequence(DP、最长公共子序列)
- HDU 1159 Common Subsequence(最长公共子序列的长度)
- HDU Common Subsequence 最长公共子序列
- HDU Common Subsequence(最长公共子序列)
- HDU 1159 Common Subsequence 公共子序列 简单DP
- POJ--1458:Common Subsequence (DP求最长公共子序列)
- POJ 1458 Common Subsequence(求最长公共子序列)
- 求最长公共子序列Longest Common Subsequence LCS
- TOJ 1683 Common Subsequence 最长公共子序列 依然DP
- 用canvas完成超炫酷的数字时钟
- android:windowSoftInputMode属性详解
- PHP持续集成实践
- java 中不常见的关键字:strictfp,transient
- Hadoop的Mapreduce运行原理详解
- (hdu step 3.2.2)Common Subsequence(简单dp:求最长公共子序列的长度)
- Java客户端调用Https Webservice
- 解读TestBird手游兼容性测试白皮书
- 用户交互体验
- 遇到问题-----虚拟机安装64位系统“此主机支持 Intel VT-x,但 Intel VT-x 处于禁用状态”的问题
- Ubuntu配置JDK和Android环境变量
- js引用赋值
- 分华国锋哈哈哈发规范和风光的很多风光好
- 开发者可以在Codewars上面学习各种编程语言,Codewars的奖励系统激励程序员们像打游戏升级一样地做习题。