HDU - 1159 Common Subsequence (最长公共子序列)
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.
Input
abcfbc abfcab
programming contest
abcd mnp
Output
4
2
0
Sample Input
abcfbc abfcab
programming contest
abcd mnp
Sample Output
4
2
0
#include <iostream>
#include <stdio.h>
#include <cstring>
#include <algorithm>
#include <string>
using namespace std;
#define N 1005
int dp[N+][N+];
char str1[N],str2[N];
int lcs(int len1,int len2)
{
int i,j;
int len=max(len1,len2);
for(i=;i<=len;i++){
dp[i][]=;
dp[][i]=;
}
for(i=;i<=len1;i++){
for(j=;j<=len2;j++){
if(str1[i-]==str2[j-]){
dp[i][j]=dp[i-][j-]+;
}
else {
dp[i][j]=max(dp[i][j-],dp[i-][j]);
}
}
}
return dp[len1][len2];
}
int main()
{
while(cin>>str1>>str2){
int len1=strlen(str1);
int len2=strlen(str2);
cout<<lcs(len1,len2)<<endl;
}
return ;
}
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> ...
随机推荐
- Yandex Big Data Essentials Week1 Unix Command Line Interface File Content exploration
cat displays the contents of a file at the command line copies or apppend text file into a document ...
- javascript原生ajax请求
class Ajax{ constructor(url, method, data, callback_suc, callback_err, callback_run){ this.RT = true ...
- PWA 推送实践
PWA 推送实践 最近公司内录任务的系统总是忘记录任务,而那个系统又没有通知,所以想要实现一个浏览器的通知功能,免得自己忘记录入任务. 前端实现通知的几种方式 想要实现通知,我们就需要有个客户端,对于 ...
- SpringCloud学习之—Eureka集群搭建
Eureka集群的搭建 上次说过了在SpringCloud应用中使用Eureka注册中心,用来对服务提供者进行服务注册与发现,但同时,它也是一个"微服务",单个应用使用空间有限,因 ...
- mysql 的root 用户无法授权,navicat 远程授权提示1044解决方案
先看解决方案 #------------mysql root 用户无法赋权问题解决 -------- ,登录 mysql -u root -p ,use mysql; 选择mysql数据库 ,执行以下 ...
- 00-django | 01-构建博客目录
00-django | 01-构建博客目录 python Django 创建blog 进入到 manage.py 文件所在的目录(即项目根目录)下,运行 pipenv run python manag ...
- cf999E (强联通分量模板题)
给出n个点m条边的有向图,问至少添加多少条边使得任何点都可以从s点出发可达 #include<bits/stdc++.h> #define forn(i, n) for (int i = ...
- NODEJS 搭建本地文件服务器
npm install anywhere --g 然后再任意目录位置运行 anywhere 80 就可以开启服务器.
- HTML表单概念、语法及创建表单,案例
form 标签 Input标签的type属性值 单行文本域 <input type="text" /> 图像域(图像提交按钮) 下拉菜单和列表标签 select 标签属 ...
- python数据类型(总结篇)
世界上最容易的事是坚持,最难的事也是坚持.开通博客园已两月有余,但实际上笔者本人的活跃度非常低,痛定思痛,自己选的路含泪也要走下去,继续坚持! 本文承接前几个月的python数据类型系列,完成对字典与 ...