HDU 1159 Common Subsequence (动规+最长公共子序列)
Common Subsequence
Total Submission(s): 22698 Accepted Submission(s): 9967
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
pid=1176" target="_blank">1176
1058 1421 1160pid=1978" target="_blank">1978
题目大意:求最长公共子序列。
在一些细节上有借鉴的地方,其它没什么了。
代码:
#include <iostream>
#include <string.h>
using namespace std;
#define M 1000
#define max(a,b) (a>b?a:b)
char ma1[M],ma2[M];
int dp[M][M];
int main(int i,int j,int k)
{
int l1,l2;
while(scanf("%s%s",ma1+1,ma2+1)!=EOF) //这是个有意思的地方,由于在后面要用动规从1->l1,所以字符串从1開始。
{
memset(dp,0,sizeof(dp));
l1=strlen(ma1+1); //strlen是測字符串长度,没办法,还要+1.
l2=strlen(ma2+1); for(i=1;i<=l1;i++) //这里就是模板。不说明了。 for(j=1;j<=l2;j++)
{
if(ma1[i]==ma2[j]) dp[i][j]=dp[i-1][j-1]+1;
else dp[i][j]=max(dp[i][j-1],dp[i-1][j]);
}
printf("%d\n",dp[l1][l2]);
}
return 0;
}
HDU 1159 Common Subsequence (动规+最长公共子序列)的更多相关文章
- HDU 1159 Common Subsequence【dp+最长公共子序列】
Common Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- POJ 1458 Common Subsequence(LCS最长公共子序列)
POJ 1458 Common Subsequence(LCS最长公共子序列)解题报告 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?c ...
- Common Subsequence POJ - 1458 最长公共子序列 线性DP
#include <iostream> #include <algorithm> #include <string> #include <cstring> ...
- 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 动态规划
2017-08-06 15:41:04 writer:pprp 刚开始学dp,集训的讲的很难,但是还是得自己看,从简单到难,慢慢来(如果哪里有错误欢迎各位大佬指正) 题意如下: 给两个字符串,找到其中 ...
- 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最长公共子序列)
Common Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
随机推荐
- 由DB2分页想到的,关于JDBC ResultSet 处理大数据量
最近在处理DB2 ,查询中,发现如下问题.如果一个查询 count(*),有几十万行,分页如何实现 select row_number() over (order by fid desc ) as r ...
- setTimeout 0
setTimeout 0 就是把事件放到下一次事件循环时调用,至少要一个时钟之后: ); console.log('this should before setTimeout 0'); var i=0 ...
- SDK_组合框的使用
组合框的使用 组合框的创建:有三种风格,分别 Simaple,Dropdown(可输入), 下拉列表(不可输入) 可以通过可视化编程中下拉列表的 下拉箭头 设置列表的长度 如何向组合框控件中添加数据, ...
- 软件工程师需要了解的网络知识:从铜线到HTTP(一)—— 前言
转自:https://lvwenhan.com/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F/485.html?hmsr=toutiao.io&utm_medium ...
- <SpringMvc>入门三 参数绑定
1.get请求 <%--请求参数的绑定--%> <%--get请求参数--%> <a href="/param/testParam1?username=tom& ...
- VNC 安装 (适用Redhat 9.0 和 CentOS 7.0+)
Remote Service 本文转自https://www.cnblogs.com/yjscloud/p/6695388.html VNC 安装 (适用Redhat 9.0 和 CentOS 7.0 ...
- Python学习第二阶段Day2(json/pickle)、 shelve、xml、PyYAML、configparser、hashlib模块
1.json/pickle 略. 2.shelve模块 import shelve # shelve 以key value的形式序列化,value为对象 class Foo(object): de ...
- git 忽略文件[.gitignore]常用配置
.idea .buildpath .project .settings .Ds_Store composer.json composer.lock a.php /public/uploads /run ...
- Django DTL模板语法中的过滤器
template_filter_demo 过滤器相关: 一.形式:小写{{ name | lower }} 二.串联:先转义文本到HTML,再转换每行到 <p> 标签{{ my_text| ...
- Balanced Numbers(数位dp)
Description Balanced numbers have been used by mathematicians for centuries. A positive integer is c ...