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

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

Common Subsequence
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 18332 Accepted Submission(s): 7742
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
Southeastern Europe 2003
Recommend
Ignatius

//78MS    4180K    653 B    C++    //dp,最长公共子序列  #include
#include
int dp[1005][1005];int Max(int a,int b){ return a>b?a:b;}int main(void){ char a[1005],b[1005]; while(scanf("%s %s",a,b)!=EOF) { int lena=strlen(a); int lenb=strlen(b); memset(dp,0,sizeof(dp)); int maxn=0; for(int i=1;i<=lena;i++) for(int j=1;j<=lenb;j++){ if(a[i-1]==b[j-1]) dp[i][j]=dp[i-1][j-1]+1; //状态转移方程 else dp[i][j]=Max(dp[i][j-1],dp[i-1][j]); if(dp[i][j]>maxn) maxn=dp[i][j]; } printf("%d\n",maxn); } return 0;}

 

转载于:https://www.cnblogs.com/GO-NO-1/articles/3330277.html

你可能感兴趣的文章
关于 top 工具的 6 个替代方案
查看>>
程序员最讨厌的9句话,你可有补充?
查看>>
PAT A1037
查看>>
浅谈RPC
查看>>
HDU 4422 The Little Girl who Picks Mushrooms(简单题)
查看>>
HDUOJ---------(1045)Fire Net
查看>>
TextView 超链接点击跳转到下一个Activity
查看>>
sql server 2008安装的时候选NT AUTHORITY\NEWORK SERVICE 还是选 NT AUTHORITY\SYSTEM ?
查看>>
UNIX环境高级编程之第4章:文件和文件夹-习题
查看>>
bzoj2843极地旅行社题解
查看>>
【Linux】Linux中常用操作命令
查看>>
MyBatis3-SqlSessionDaoSupport的使用
查看>>
ReactiveSwift源码解析(三) Signal代码的基本实现
查看>>
MVC模式利用xib文件定制collectionCell
查看>>
(六)Oracle学习笔记—— 约束
查看>>
【SQL】查询数据库中某个字段有重复值出现的信息
查看>>
mysql 行转列 列转行
查看>>
[Oracle]如何在Oracle中设置Event
查看>>
top.location.href和localtion.href有什么不同
查看>>
02-创建hibernate工程
查看>>