HDU 1159 Common Subsequence 最长公共子序列
来源:互联网 发布:义隆单片机型号 编辑:程序博客网 时间:2024/06/05 17:19
点击打开链接
Common Subsequence
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 15540 Accepted Submission(s): 6469
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
动态规划中典型的LCS问题。只要把握如下核心:
if(s1[i]==s2[j])
len[i][j]=len[i-1][j-1]+1
else
len[i][j]=max(len[i-1][j],len[i][j-1])
用一个矩阵表示,举个例子,比如programn contest
p r o g r a m n
0 0 0 0 0 0 0 0
c 0 0 0 0 0 0 0 0 0
o 0 0 0 1 1 1 1 1 1
n 0 0 0 1 1 1 1 1 2
t 0 0 0 1 1 1 1 1 2
e 0 0 0 1 1 1 1 1 2
s 0 0 0 1 1 1 1 1 2
t 0 0 0 1 1 1 1 1 2
最后直接输出len[MAX_LEN][MAX_LEN]
#include<stdio.h>#include<string.h>char s1[1007],s2[1007];int dp[1007][1007];int max(int x,int y){ return x>y?x:y;}int main(){ while(scanf("%s%s",s1+1,s2+1)!=EOF) { int len1,len2,i,j; len1=strlen(s1+1); len2=strlen(s2+1); for(i=0;i<=len1;i++) dp[i][0]=0; for(j=0;j<=len2;j++) dp[0][j]=0; for(i=1;i<=len1;i++) for(j=1;j<=len2;j++) { if(s1[i]==s2[j]) dp[i][j]=dp[i-1][j-1]+1; else dp[i][j]=max(dp[i-1][j],dp[i][j-1]); } printf("%d\n",dp[len1][len2]); } return 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(最长公共子序列)
- 数组的常见的排序及查找操作
- Web.xml配置详解
- 实例学习之仿点点博客
- GDI+ 画透明背景的字
- Ubuntu安装基础教程
- HDU 1159 Common Subsequence 最长公共子序列
- [MFC] Combo Box 控件向 Edit Contrl控件传输Combo Box下拉选项的字符串 [大三TJB_708]
- linux 页表
- 1、Alfresco 4.2.c开发环境的搭建-- alfresco 开发系列
- 编码问题
- 问题十五:定义一个结构体变量(包括年、月、日),输入两个人的生日,求出他们相差多少天。
- Ruby on Rails Resource
- js中call与apply用法
- 微软实习生笔试总结: