hdu 1159 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.
programming contest
abcd mnp
2
0
题意:给两个字符串,求这两个字符串的最长公共子序列的长度。
序列特点:最长公共子序列中的字符出现的顺序和两个母串中出现的顺序是一样的。而最长公共子串则要求的更严格一点,子串中字符出现的顺序和母串中出现的顺序都必须是连续的;
代码如下:
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<ctype.h>
#include<algorithm>
#define N 1010
using namespace std;
int dp[1010][1010];
int main ()
{
int n,m,i,j;
int len1,len2;
char a[N],b[N];
a[0]='-';
b[0]='-';
while(~scanf("%s %s",a+1,b+1)){//使得存的字符串从字符串的地二位开始存;
len1=strlen(a);
len2=strlen(b);
for(i=1;i<max(len1,len2);i++){//初始化
dp[i][0]=dp[0][i]=0;
}
for(i=1;i<len1;i++){
for(j=1;j<len2;j++){
if(a[i]==b[j]){
dp[i][j]=dp[i-1][j-1]+1;
}else {
dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
}
}
}
/*for(i=0;i<len1;i++){
for(j=0;j<len2;j++){
printf("%d ",dp[i][j]);
}
printf("\n");
}*/
printf("%d\n",dp[len1-1][len2-1]);
}
return 0;
}
hdu 1159 Common Subsequence (最长公共子序列 +代码)的更多相关文章
- HDU 1159 Common Subsequence 最长公共子序列
HDU 1159 Common Subsequence 最长公共子序列 题意 给你两个字符串,求出这两个字符串的最长公共子序列,这里的子序列不一定是连续的,只要满足前后关系就可以. 解题思路 这个当然 ...
- hdu 1159 Common Subsequence(最长公共子序列 DP)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159 Common Subsequence Time Limit: 2000/1000 MS (Jav ...
- C++版 - Lintcode 77-Longest Common Subsequence最长公共子序列(LCS) - 题解
版权声明:本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C++版 - L ...
- POJ 1458 Common Subsequence(最长公共子序列LCS)
POJ1458 Common Subsequence(最长公共子序列LCS) http://poj.org/problem?id=1458 题意: 给你两个字符串, 要你求出两个字符串的最长公共子序列 ...
- lintcode 77.Longest Common Subsequence(最长公共子序列)、79. Longest Common Substring(最长公共子串)
Longest Common Subsequence最长公共子序列: 每个dp位置表示的是第i.j个字母的最长公共子序列 class Solution { public: int findLength ...
- LCS(Longest Common Subsequence 最长公共子序列)
最长公共子序列 英文缩写为LCS(Longest Common Subsequence).其定义是,一个序列 S ,如果分别是两个或多个已知序列的子序列,且是所有符合此条件序列中最长的,则 S 称为已 ...
- LCS修改版(Longest Common Subsequence 最长公共子序列)
题目描述 作为一名情报局特工,Nova君(2号)有着特殊的传达情报的技巧.为了避免被窃取情报,每次传达时,他都会发出两句旁人看来意义不明话,实际上暗号已经暗含其中.解密的方法很简单,分别从两句话里删掉 ...
- POJ 1458 Common Subsequence 最长公共子序列
题目大意:求两个字符串的最长公共子序列 题目思路:dp[i][j] 表示第一个字符串前i位 和 第二个字符串前j位的最长公共子序列 #include<stdio.h> #include&l ...
- LCS(Longest Common Subsequence)最长公共子序列
最长公共子序列(LCS)是一个在一个序列集合中(通常为两个序列)用来查找所有序列中最长子序列的问题.这与查找最长公共子串的问题不同的地方是:子序列不需要在原序列中占用连续的位置 .最长公共子序列问题是 ...
- PKU 1458 Common Subsequence(最长公共子序列,dp,简单)
题目 同:ZJU 1733,HDU 1159 #include <stdio.h> #include <string.h> #include <algorithm> ...
随机推荐
- UVA796 Critical Links(求桥) 题解
题意:求桥 思路:求桥的条件是:(u,v)是父子边时 low[v]>dfn[u] 所以我们要解决的问题是怎么判断u,v是父子边(也叫树枝边).我们在进行dfs的时候,要加入一个fa表示当前进行搜 ...
- Gym - 100345H Settling the Universe Up(bitset)
https://vjudge.net/problem/Gym-100345H 题意: 给出一个图,求图中u能到达v的对数,并且u<v.并且会有更新和查询操作. 思路: bitset直接暴力,对于 ...
- MVC ---- ckeditor 批量绑定 blur 事件
在项目遇到个问题,就是把循环出来的ckeditor 批量添加 blur 事件,折腾了2天 终于搞定 @{ ].Rows) { <table class="ui-jqgrid-btabl ...
- lncRNA研究利器之"TANRIC"
http://bioinformatics.mdanderson.org/main/Main_Page http://ibl.mdanderson.org/tanric/_design/basic/i ...
- windows 命令行中使用winrar
加入环境变量即可 我的电脑右键属性->高级->环境变量->系统变量->编辑 变量名:path 变量值:C:/Program Files/WinRAR; --变量值为WinRAR ...
- python 深拷贝
from copy import deepcopy #create a tuple tuplex = (, [], True) print(tuplex) #make a copy of a tupl ...
- MVC扩展Url.Action方法解决复杂对象参数问题
1:问题描述 @Url.Action("Index", "Home", new { Key = "Key", Val = new { Nam ...
- MongoDB(课时30 $group)
3.7.5.聚合框架(核心) MapReduce功能强大,但是它的复杂度和功能一样强大,那么我们需要MapReduce的功能,使用聚合框架中的聚合函数:aggregate(). 3.7.5.1.gro ...
- PL/SQL Developer-官网下载地址
官网下载地址:https://www.allroundautomations.com/registered/plsqldev.html
- 记录Vmware Workstation及Centos6.8 的安装
网上找到的一个非常详细的安装详解.个人觉得非常好.于是加保存一下.地址:http://www.mamicode.com/info-detail-1462939.html