ZOJ 1733 Common Subsequence(LCS)
Common Subsequence
Time Limit: 2 Seconds Memory Limit: 65536 KB
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
分析:最长公共子序列
代码如下:
# include<stdio.h>
# include<string.h>
# define MAX
char s1[MAX],s2[MAX];
int dp[MAX][MAX];
int len1,len2;
int max(int a,int b,int c){
int temp;
temp = a>b ? a : b;
return temp>c ? temp : c;
}
int main(){
int i,j;
while(scanf("%s%s",s1,s2)!=EOF){
len1 = strlen(s1);
len2 = strlen(s2);
memset(dp,,sizeof(dp));
for(i=;i<=len1;i++){
for(j=;j<=len2;j++){
if(s1[i-] == s2[j-])
dp[i][j] = dp[i-][j-] + ;
dp[i][j] = max(dp[i][j],dp[i-][j],dp[i][j-]);
}
}
printf("%d\n",dp[len1][len2]);
}
return ;
}
ZOJ 1733 Common Subsequence(LCS)的更多相关文章
- Longest common subsequence(LCS)
问题 说明该问题在生物学中的实际意义 Biological applications often need to compare the DNA of two (or more) different ...
- hdu 1159 Common Subsequence(LCS)
Common Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- poj 1458 Common Subsequence ——(LCS)
虽然以前可能接触过最长公共子序列,但是正规的写应该还是第一次吧. 直接贴代码就好了吧: #include <stdio.h> #include <algorithm> #inc ...
- POJ 1458 Common Subsequence(LCS最长公共子序列)
POJ 1458 Common Subsequence(LCS最长公共子序列)解题报告 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?c ...
- HDUOJ ---1423 Greatest Common Increasing Subsequence(LCS)
Greatest Common Increasing Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536 ...
- HDU 1159:Common Subsequence(LCS模板)
Common Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- HDU 1159 Common Subsequence(裸LCS)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1159 Common Subsequence Time Limit: 2000/1000 MS (Jav ...
- hdu 1159:Common Subsequence(动态规划)
Common Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- HDU 1159 Common Subsequence (dp)
题目链接 Problem Description A subsequence of a given sequence is the given sequence with some elements ...
随机推荐
- vss搭建于操作
1.下载的vvs2005,下载后先安装在服务器上,反正就是下一步下一步就对了 安装完成后,打开miscrosoft visual sourcesafe,---create connection da ...
- c语音中打印参数调用层级即call stack, call trace
http://stackoverflow.com/questions/105659/how-can-one-grab-a-stack-trace-in-c There's backtrace(), a ...
- dp hdu-4433 locker
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4433 题目大意: 给两个长度相等的数字串s1,s2.每次操作可以把连续的最多三位都+1或-1,如果超 ...
- 2013腾讯编程马拉松初赛第二场(3月22日) 小Q系列故事——为什么时光不能倒流 ---好水!!
我以为我会是最坚强的那一个 我还是高估了自己 我以为你会是最无情的那一个 还是我贬低了自己 就算不能够在一起 我还是为你担心 就算你可能听不清 也代表我的心意 那北极星的眼泪 闪过你曾经的眼角迷离 那 ...
- JSBinding+Bridge:逻辑代码中操作二进制数据
以这2个函数为例 class File { public static byte[] ReadAllBytes(string path); public static void WriteAllByt ...
- java利用commons-email发送邮件并进行封装
本例中利用commons-email发送邮件并进行封装,支持html内容和附件:Commons Email是Apache的Commons子项目下的一个邮件客户端组件,它是基于JavaMail的,大大简 ...
- Hibernate查询之Criteria查询
转自:http://www.cnblogs.com/Laupaul/archive/2012/02/15/2353194.html Criteria是一种比hql更面向对象的查询方式.Criteria ...
- Plinq-Parallel.ForEach for 性能提升
https://msdn.microsoft.com/zh-cn/library/dd460720.aspx 本示例显示如何使用 Parallel.ForEach 循环对任何 System.Colle ...
- spring 最全MAVEN 依赖引入配置
<properties> <spring.version>4.1.6.RELEASE</spring.version> </properties> &l ...
- CAS+SSO原理浅谈
http://www.cnblogs.com/yonsin/archive/2009/08/29/1556423.htmlSSO 是一个非常大的主题,我对这个主题有着深深的感受,自从广州 UserGr ...