博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU1159 Common Subsequence【最长公共子序列】
阅读量:6992 次
发布时间:2019-06-27

本文共 1751 字,大约阅读时间需要 5 分钟。

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
code:
View Code
#include
#include
int main() {
char s1[1000],s2[1000]; //输入两个字符串序列 int r[1000],s[1000]; //DP记录状态转移结果,采用滚动数组节省空间 int l1,l2,i,j; //l1,l2记录两个字符串的长度 while(scanf("%s%s",s1,s2)!=EOF) {
l1=strlen(s1); l2=strlen(s2); for(i=0;i<=l2;i++) r[i]=0; for(i=0;i
s[j]?r[j+1]:s[j]; //如果序列对应字符不同,取大的一个 for(j=1;j<=l2;j++) r[j]=s[j]; //数组往后滚动 } printf("%d\n",r[l2]); //最大的字符串长度值是最后的元素值 } return 0; }

转载于:https://www.cnblogs.com/dream-wind/archive/2012/03/14/2396283.html

你可能感兴趣的文章
BZOJ2743:[HEOI2012]采花——题解
查看>>
android webview 视频相关
查看>>
oracle数据集合的操作
查看>>
[转] word2vec
查看>>
欧洲大陆
查看>>
基于Java反射的map自动装配JavaBean工具类设计
查看>>
文件复制
查看>>
svn创建版本库和删除版本库
查看>>
Android系统联系人全特效实现(上),分组导航和挤压动画
查看>>
ATS项目更新(4) 更新DLL到远程服务器
查看>>
Bzoj 1426 收集邮票
查看>>
mysql面试题
查看>>
mac 多显示器焦点快速切换
查看>>
第六周学习进度报告
查看>>
【ASP.NET开发】ASP.NET(MVC)三层架构知识的学习总结 分类: ...
查看>>
[译]ZOOKEEPER RECIPES-Leader Election
查看>>
微信小程序用户数据解密
查看>>
nginx发布静态网页
查看>>
Hadoop 面试题之一
查看>>
有关方法重载的实例(例4-10)
查看>>