hdu-Common Subsequence
http://acm.hdu.edu.cn/showproblem.php?pid=1159
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
最长公共子序列问题具有最优子结构性质
设
X = { x1 , ... , xm }
Y = { y1 , ... , yn }
及它们的最长子序列
Z = { z1 , ... , zk }
则
1、若 xm = yn , 则 zk = xm = yn,且Z[k-1] 是 X[m-1] 和 Y[n-1] 的最长公共子序列
2、若 xm != yn ,且 zk != xm , 则 Z 是 X[m-1] 和 Y 的最长公共子序列
3、若 xm != yn , 且 zk != yn , 则 Z 是 Y[n-1] 和 X 的最长公共子序列
由性质导出子问题的递归结构
当 i = 0 , j = 0 时 , c[i][j] = 0
当 i , j > 0 ; xi = yi 时 , c[i][j] = c[i-1][j-1] + 1
当 i , j > 0 ; xi != yi 时 , c[i][j] = max { c[i][j-1] , c[i-1][j] }
注意边界,两个串的下标都是从1开始的,因为递归时是以0为结束标志的~
dp[i][j] 表示的是 表示长度为i的字符串和长度为j的字符串的最大公共子串的长度
分析:
#include<iostream>
#include<string.h>
using namespace std;
char str1[1005];
char str2[1005];
int dp[1005][1005];
int main()
{
while(scanf("%s %s",str1+1,str2+1)!=EOF)
{
memset(dp,0,sizeof(dp));//这一步就已经初始化边界了~ dp[0][j:1->len2]=0;
int len1=strlen(str1+1),len2=strlen(str2+1);
for(int i=1;i<=len1;i++)
for(int j=1;j<=len2;j++)
{
if(str1[i]==str2[j])
dp[i][j]=dp[i-1][j-1]+1;
else
dp[i][j]=max(dp[i-1][j],dp[i][j-1]); }
cout<<dp[len1][len2]<<endl; }
return 0;
}
另:
#include<iostream>
#include<cstring>
using namespace std;
int max(int a,int b)
{
return a>b?a:b;
}
int main()
{
int i,j,dp[500][500];
string s1,s2;
while(cin>>s1>>s2)
{
memset(dp,0,sizeof(dp));
for(i=1;i<=s1.size();i++)
{
for(j=1;j<=s2.size();j++)
{
if(s1[i-1]==s2[j-1])dp[i][j]=dp[i-1][j-1]+1;
else dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
}
}
cout<<dp[s1.size()][s2.size()]<<endl;
}
return 0;
}
hdu-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【dp+最长公共子序列】
Common Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- 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 http://acm.hust.edu.cn/vjudge/contest/view.action ...
- 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 最长公共子序列
HDU 1159 Common Subsequence 最长公共子序列 题意 给你两个字符串,求出这两个字符串的最长公共子序列,这里的子序列不一定是连续的,只要满足前后关系就可以. 解题思路 这个当然 ...
- hdu 1159:Common Subsequence(动态规划)
Common Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- hdu 1159 Common Subsequence(LCS最长公共子序列)
Common Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
随机推荐
- js点击更多显示更多内容效果
我写了一个简单的分段显示插件,用法很简单:1,把你要分面显示的内容的容器元素增加一个class=showMoreNChildren,并增加一个自定义属性pagesize="8" 这 ...
- org.gradle.process.internal.ExecException:
com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process ...
- linux----命令替换
0.命令替换.它的大概意思是.命令在脚本中只起一个站位符的作用:在命令运行时它会被命令自己的执行结果所替换. 1.使用格式: 1.$(command) 2.`command` 2.使用举例: 1.:打 ...
- CSS---input标签注意
总结一下,在给input标签写CSS时需要注意的有以下几点: 一.不要给属性为text的input标签设置高度,这样无法让IE浏览器下输入框中的文字垂直居中显示.尽管你后来想要通过设置padding属 ...
- J2SE知识点摘记(十九)
Collection 1.2.1 常用方法 Collection 接口用于表示任何对象或元素组.想要尽可能以常规方式处理一组元素时,就使用这一接口.Collection 在前面的大图也 ...
- Oracle EBS-SQL (BOM-7):检查有BOM无工艺路线的子装配件或成品.sql
select msi.segment1, msi.description, msi.item_typefrom inv.mtl_system_items_b msiwher ...
- 交通银行万事达Y-POWER信用卡 普卡
签账消费 免息尽享 失卡保护 风险全无 密码签名 任选 境外使用 本币还款 国内海外 环球支持 适合人群:年轻一族 发行状态:发行中 年费: 140元 币种: 人民币+美元 免年费政策:免首年 ...
- Gridland(规律)
Gridland Time Limit: 2 Seconds Memory Limit: 65536 KB BackgroundFor years, computer scientists ...
- Permutation Recovery(模拟)
Permutation Recovery Time Limit: 10000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- c++基础 之 面向对象特征一 : 继承
class Base { public: void f() { cout<<"void f()"<<endl<<endl; } void f(i ...