HDU Common Subsequence(最长公共子序列)
来源:互联网 发布:下载有声小说的软件 编辑:程序博客网 时间:2024/06/06 00:51
Common Subsequence
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 32838 Accepted Submission(s): 14875
Total Submission(s): 32838 Accepted Submission(s): 14875
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.
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 | We have carefully selected several similar problems for you: 1087 1176 1003 1058 1069
最长公共子序列问题,简单的动态规划
1处纵轴比到f横轴比到第二个b,从前面序列看,自然为3,和上一位f对f相等
2处横轴比到b,纵轴比到最后一个c,从序列上看,自然与纵轴上一个f相等,因为比到此处的cb处最多也就为2了。
在计算最优解的过程中,可以用另外一个二维数组来表示每次选择,用c[i][j]=1表示result[i][j]是由result[i-1][j-1]得来的,用c[i][j]=2表示result[i][j]是由result[i-1][j]得来的,由c[i][j]=3表示result[i][j]是由result[i][j-1]得来的。
根据c数组的值,可以倒着从最后一步步推出每步的选择,进而确定出公共子序列中的元素。
最长公共子序列问题,简单的动态规划
1处纵轴比到f横轴比到第二个b,从前面序列看,自然为3,和上一位f对f相等
2处横轴比到b,纵轴比到最后一个c,从序列上看,自然与纵轴上一个f相等,因为比到此处的cb处最多也就为2了。
3处比到cc,加上上处bf自然为3
反正看表多看几个特殊值感受下吧,感觉哪里没有完全明白
反正看表多看几个特殊值感受下吧,感觉哪里没有完全明白
#include <iostream>#include <cstring>using namespace std;int ans[1005][1005];int main(){ char a[1005],b[1005]; int i,j; while(cin>>a>>b){ int len1=strlen(a); int len2=strlen(b); for(i=0;i<=len1;i++) ans[i][0]=0; for(j=0;j<=len2;j++) ans[0][j]=0; for(i=1;i<=len1;i++) { for(j=1;j<=len2;j++) { if(a[i-1]==b[j-1]) { ans[i][j]=ans[i-1][j-1]+1; } else { ans[i][j]=ans[i-1][j]>=ans[i][j-1]?ans[i-1][j]:ans[i][j-1];//为什么是上和左都取,因为这个横纵轴坐标是相互的,可互换 } } } cout<<ans[len1][len2]<<endl; } return 0;}
在计算最优解的过程中,可以用另外一个二维数组来表示每次选择,用c[i][j]=1表示result[i][j]是由result[i-1][j-1]得来的,用c[i][j]=2表示result[i][j]是由result[i-1][j]得来的,由c[i][j]=3表示result[i][j]是由result[i][j-1]得来的。
根据c数组的值,可以倒着从最后一步步推出每步的选择,进而确定出公共子序列中的元素。
0 0
- HDU Common Subsequence 最长公共子序列
- HDU Common Subsequence(最长公共子序列)
- hdu 1159 Common Subsequence ***poj1458(最长公共子序列)
- hdu 1159 Common Subsequence 最长公共子序列
- hdu 1159 Common Subsequence(DP最长公共子序列)
- HDU 1159 Common Subsequence 最长公共子序列
- HDU 1159 Common Subsequence--最长公共子序列
- hdu 1159 Common Subsequence (最长公共子序列)
- Common Subsequence 最长公共子序列 hdu 1159
- hdu 1159 Common Subsequence(LCS最长公共子序列)
- hdu 1159 Common Subsequence(最长公共子序列)
- hdu 1159 Common Subsequence(动态规划:最长公共子序列)
- HDU 1159 Common Subsequence (动规+最长公共子序列)
- HDU 1159:Common Subsequence(最长公共子序列)
- 最长公共子序列DP Common Subsequence HDU 1159
- hdu 1159 Common Subsequence(最长公共子序列)
- HDU 1159 Common Subsequence(最长公共子序列)
- HDU 1159 Common Subsequence(最长公共子序列)
- 【Linux】解决SSH服务拒绝密码
- 正则表达式语法
- Android基于cordova3.3的插件开发
- 科技节图像后期处理三等奖作品---陪伴是最长情的告白
- 关于代码直接调用高德地图和百度地图导航功能的代码
- HDU Common Subsequence(最长公共子序列)
- ViewGroup setVisibility 为GONE 子View依然占用地方,其中的子EditText会出现点击焦点占用
- Lucene 索引的并发读写
- 最小生成树 套餐
- etcd:从应用场景到实现原理的全方位解读
- [Special] Design Pattern - Structural Patterns - Proxy Pattern
- 数据库之SQL 数据类型转换 使用cast()函数
- HTML5--02 HTML5的基本文本
- 第一个C++程序