hdu 1159:Common Subsequence(动态规划)
Common Subsequence
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 18765 Accepted Submission(s): 7946
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.
programming contest
abcd mnp
2
0
#include <iostream> using namespace std;
int dp[][];
int main()
{
//dp[i][j]代表着a取前i个字符和b取前j个字符时的最长公共子序列的大小
char a[],b[];
while(cin>>a>>b){
int i,j;
int al,bl;
for(i=;a[i]!='\0';i++); //计算a、b字符串长度
for(j=;b[j]!='\0';j++);
al=i;bl=j; for(i=;i<=al;i++) //dp[][]初始化
dp[i][]=;
for(i=;i<=bl;i++)
dp[][i]=; for(i=;i<=al;i++) //计算dp[][]
for(j=;j<=bl;j++){
if(a[i-]==b[j-])
dp[i][j]=dp[i-][j-]+;
else
dp[i][j] = dp[i-][j] > dp[i][j-] ? dp[i-][j] : dp[i][j-];
} cout<<dp[al][bl]<<endl;
}
return ;
}
#include <stdio.h>
#include <stdlib.h>
int dp[][];
int main()
{
char a[],b[];
while(scanf("%s%s",a,b)!=EOF){
int i,j;
int al,bl;
for(i=;a[i]!='\0';i++);
for(j=;b[j]!='\0';j++);
al=i;bl=j;
for(i=;i<=al;i++)
dp[i][]=;
for(j=;j<=bl;j++)
dp[][j]=;
for(i=;i<=al;i++)
for(j=;j<=bl;j++){
if(a[i-]==b[j-])
dp[i][j] = dp[i-][j-]+;
else
dp[i][j] = dp[i-][j] > dp[i][j-] ? dp[i-][j] : dp[i][j-];
}
printf("%d\n",dp[al][bl]);
}
return ;
}
Freecode : www.cnblogs.com/yym2013
hdu 1159:Common Subsequence(动态规划)的更多相关文章
- HDU 1159 Common Subsequence 动态规划
2017-08-06 15:41:04 writer:pprp 刚开始学dp,集训的讲的很难,但是还是得自己看,从简单到难,慢慢来(如果哪里有错误欢迎各位大佬指正) 题意如下: 给两个字符串,找到其中 ...
- HDU 1159 Common Subsequence 最长公共子序列
HDU 1159 Common Subsequence 最长公共子序列 题意 给你两个字符串,求出这两个字符串的最长公共子序列,这里的子序列不一定是连续的,只要满足前后关系就可以. 解题思路 这个当然 ...
- HDU 1159 Common Subsequence
HDU 1159 题目大意:给定两个字符串,求他们的最长公共子序列的长度 解题思路:设字符串 a = "a0,a1,a2,a3...am-1"(长度为m), b = "b ...
- 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(最长公共子序列)
题目链接: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 ...
- 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 (poss ...
随机推荐
- 设置button不同状态下的背景色,即把这个颜色变成图片设置成,背景图片
- (void)setBackgroundColor:(UIColor *)backgroundColor forState:(UIControlState)state { [self setBack ...
- jquery源码分析-工具函数
jQuery的版本一路狂飙啊,现在都到了2.0.X版本了.有空的时候,看看jquery的源码,学习一下别人的编程思路还是不错的. 下面这里是一些jquery的工具函数代码,大家可以看看,实现思路还是很 ...
- JS的运行机制
代码块: JS中的代码块是指由<script>标签分割的代码段.JS是按照代码块来进行编译和执行的,代码块间相互独立(即就算代码块1出错,但不影响代码块2的加载和执行),但变量和方法共享. ...
- XSS 探索
1. 什么是XSS攻击? 正常的页面被渗出了攻击者的js脚本,这些脚本可以非法地获取用户信息,然后将信息发送到attacked的服务端. XSS是需要充分利用输出环境来构造攻击脚本的 2. 危害 非法 ...
- 删除/var/lib/docker
FATA[0000] Get http:///var/run/docker.sock/v1.18/containers/json?all=1: dial unix /var/run/docker.so ...
- [Effective JavaScript 笔记] 第5条:避免对混合类型使用==运算符
“1.0e0”=={valueOf:function(){return true;}} 是值是多少? 这两个完全不同的值使用==运算符是相等的.为什么呢?请看<[Effective JavaSc ...
- [POJ1003]Hangover
[POJ1003]Hangover 试题描述 How far can you make a stack of cards overhang a table? If you have one card, ...
- svn报错 400 Bad Request
MyEclipse中的svn,commit经常报错 Error: Commit failed (details follow): Error: At least one property chang ...
- tomcat配置文件之Server.xml
Server.xml包含的元素有<Server>.<Service>.<Connector>.<Engine>.<Host>.<Con ...
- qcow2文件压缩
qemu-img convert -O qcow2 /path/old.img.qcow2 /path/new.img.qcow2 转自:https://havee.me/linux/2011-09/ ...