博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu 1159 Common Subsequence
阅读量:6239 次
发布时间:2019-06-22

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

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
 
Source

 

题解:真心坑死....自己好不细心.....把dp[][]定义成了char类型....

 

代码:

1 #include 
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 #include
11 #include
12 using namespace std;13 14 char a[1001],b[1001];15 int dp[1001][1001];16 17 int LCS(int n,int m){18 int i,j;19 int len=max(n,m);20 for(i=0;i<=len;i++){21 dp[i][0]=0;22 dp[0][i]=0;23 }24 for(i=1;i<=n;i++){25 for(j=1;j<=m;j++){26 if(a[i-1]==b[j-1]){27 dp[i][j]=dp[i-1][j-1]+1;28 }29 else{30 dp[i][j]=max(dp[i-1][j],dp[i][j-1]);31 }32 }33 }34 return dp[n][m];35 }36 37 int main()38 {39 while(~scanf("%s%s",&a,&b)){40 int n=strlen(a);41 int m=strlen(b);42 int i,j;43 int len=max(n,m);44 for(i=0;i<=len;i++){45 dp[i][0]=0;46 dp[0][i]=0;47 }48 for(i=1;i<=n;i++){49 for(j=1;j<=m;j++){50 if(a[i-1]==b[j-1]){51 dp[i][j]=dp[i-1][j-1]+1;52 }53 else{54 dp[i][j]=max(dp[i-1][j],dp[i][j-1]);55 }56 }57 }58 printf("%d\n",dp[n][m]);59 /*int i=n-1,j=m-1,count=k;60 while(count!=0){61 if(a[i]==b[j]){62 cout<
dp[i-1][j]){68 j--;69 }70 else{71 i--;72 }73 }74 cout<

 

转载于:https://www.cnblogs.com/wangmengmeng/p/5019558.html

你可能感兴趣的文章
利用webmin修改超级管理员root用户登陆密码
查看>>
ENode 2.0 - 整体架构介绍
查看>>
solr长文本搜索问题
查看>>
Redis客户端Jedis(一)
查看>>
iOS学习之应用偏好设置
查看>>
手把手玩转win8开发系列课程(26)
查看>>
森林、树与二叉树相互转换
查看>>
Spark随谈(一)—— 总体架构
查看>>
算法系列15天速成——第十四天 图【上】
查看>>
django 快速实现登录
查看>>
导入数据时遇见ORA-00054
查看>>
模拟终端打印效果特效
查看>>
forfiles命令批量删除N天前文件
查看>>
顺序队列
查看>>
(NO.00005)iOS实现炸弹人游戏(三):从主场景类谈起
查看>>
git/github初级运用自如
查看>>
《Netty 权威指南》—— NIO类库简介
查看>>
Codeforces 452 A. Eevee
查看>>
小鱼儿CTO赵兴国:基于阿里云的互联网+视频会议系统实践
查看>>
基于smack的即时聊天系统之文件传输功能实现
查看>>