hdu 1159(Common Subsequence)简单dp,求出最大的公共的字符数
Common Subsequence
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.
abcfbc abfcab
programming contest
abcd mnp
4
2
0
#include<stdio.h>
#include<string.h>
int i,j;
int c[1010][1010];//c数组用来表示对于n,m,的 两个字符串。最大公共字符数
char a[1010],b[1010];//用来存储两个字符串
int lcs(int n,int m)
{
memset(c,0,sizeof(c));//初始化
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
if(a[i-1]==b[j-1])//两者同样的时候,就是在原来的基础上再加一
c[i][j]=c[i-1][j-1]+1;
else
c[i][j]=c[i-1][j]>c[i][j-1]? c[i-1][j]:c[i][j-1];//两者不同九江前面的两个字符串中的存储的最大的公共字符数存储在c[i][j]里
}
}
return c[n][m];//返回n个字符,m个字符的最大的公共字符数
}
int main()
{
while(~scanf("%s %s",a,b))
{
i=strlen(a);
j=strlen(b);
printf("%d\n",lcs(i,j));
}
return 0;
}
hdu 1159(Common Subsequence)简单dp,求出最大的公共的字符数的更多相关文章
- HDU 1159 Common Subsequence【dp+最长公共子序列】
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 ...
- hdu 1159 Common Subsequence (dp乞讨LCS)
Common Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- HDU 1159——Common Subsequence(DP)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159 题解 #include<iostream> #include<cstring> ...
- HDU 1159 Common Subsequence
HDU 1159 题目大意:给定两个字符串,求他们的最长公共子序列的长度 解题思路:设字符串 a = "a0,a1,a2,a3...am-1"(长度为m), b = "b ...
- HDU 1159 Common Subsequence 最长公共子序列
HDU 1159 Common Subsequence 最长公共子序列 题意 给你两个字符串,求出这两个字符串的最长公共子序列,这里的子序列不一定是连续的,只要满足前后关系就可以. 解题思路 这个当然 ...
- HDOJ 1159 Common Subsequence【DP】
HDOJ 1159 Common Subsequence[DP] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...
- hdu 1159 Common Subsequence(最长公共子序列 DP)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159 Common Subsequence Time Limit: 2000/1000 MS (Jav ...
- HDU 1159 Common Subsequence 公共子序列 DP 水题重温
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159 Common Subsequence Time Limit: 2000/1000 MS (Jav ...
随机推荐
- 二十六个月Android学习工作总结【转】
原文:二十六个月Android学习工作总结 1.客户端的功能逻辑不难,UI界面也不难,但写UI花的时间是写功能逻辑的两倍. 2.写代码前的思考过程非常重要,即使在简单的功能,也需要在本子上把该 ...
- Babel的配置和使用
自从 Babel 由版本5升级到版本6后,在安装和使用方式上与之前大相径庭,于是写了这篇入坑须知,以免被新版本所坑. 坑一:本地安装和全局安装 全局安装只需: $ npm install --glob ...
- java copy array
13down voteaccepted Here's a java 1.4 compatible 1.5-liner: int[] array = { 1, 2, 3, 4, 5 }; int siz ...
- Uva 11077 Find the Permutation
可以发现最优的方案就是一个循环节内互换. 所以一个有n个元素,c个循环节的置换的交换次数(最少)是n-c. 然后就可以递推了,把i插入到前i-1个元素构成的置换中,要么新成立一个循环,要么加入到之前的 ...
- 八. 输入输出(IO)操作6.文件与目录管理
目录是管理文件的特殊机制,同类文件保存在同一个目录下不仅可以简化文件管理,而且还可以提高工作效率.Java 语言在 java.io 包中定义了一个 File 类专门用来管理磁盘文件和目录. 每个 Fi ...
- InputSplit—>RecordReder—>map(key,value,context)的过程解析
上图首先描述了在TaskTracker端Task(MapTask.ReduceTask)的执行过程,MapTask(org.apache.hadoop.mapred)首先被TaskRunner调用,然 ...
- linux命令详解:df命令
转:http://www.cnblogs.com/lwgdream/p/3413579.html 前言 df命令用来查看系统的space和inode使用情况,也是常用命令之一 使用说明 -a 显示所有 ...
- 单核时代,PHP之类多线程或者多进程的,是怎么处理并发的?是排队吗?
答案是:的确就是排队.但是并不是一定要处理完请求1才能去处理请求2:实际上请求的处理过程中,有很多的时间是耗在IO等其他地方,这时可以切换去处理其他请求,把等待的时间可以充分利用起来,达到更高的吞 ...
- MessageFormat.format 字符串的模板替换
项目目前在消息的模版,模版中需要替换很多参数,比方说“用户名”,“日期”等等.不过目前没有想到更好的替换参数的方法,所以目前只能使用一个比较简单的方式来实现.这个方式太死板,参数对应必须要在代码中写死 ...
- GlusterFS分布式文件系统部署
GlusterFS是一个可伸缩的网络文件系统,使用常见的现成的硬件,您可以创建大型分布式存储流媒体解决方案.数据分析.和其他数据相关的任务.GlusterFS是自由和开源软件. 详细参考官网:http ...