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> ...
随机推荐
- linux下使用gdb对php源码调试
title: linux下使用gdb对php源码调试 date: 2018-02-11 17:59:08 tags: --- linux下使用gdb进行php调试 调试了一些php的漏洞,记录一下大概 ...
- NR / 5G - The Round Robin algorithm
- pytorch之 Variable
import torch from torch.autograd import Variable # Variable in torch is to build a computational gra ...
- 杭电-------2098 分拆素数和(c语言写)
#include<stdio.h> #include<math.h> ] = { , }; ;//全局变量,用来标志此时已有多少个素数 int judge(int n) {// ...
- Centos7.5中Nginx报错:nginx: [error] invalid PID number "" in "/run/nginx.pid" 解决方法
服务器重启之后,执行 nginx -t 是OK的,然而在执行 nginx -s reload 的时候报错 nginx: [error] invalid PID number "" ...
- PMP--2.1 商业论证(经济可行性研究报告)
####################################################### PS:半个月没有更新文档,因为是有点单线程,在整理启动过程组和规划过程组的内容,在规划规 ...
- .net代码实现上千次ping的实现
先上代码: 多线程实现ping校验: public void PingCameraNew(List<CameraMongoDto> assetlist) { ThreadPool.SetM ...
- asp.net网站作为websocket服务端的应用该如何写
最近被websocket的一个问题困扰了很久,有一个需求是在web网站中搭建websocket服务.客户端通过网页与服务器建立连接,然后服务器根据ip给客户端网页发送信息. 其实,这个需求并不难,只是 ...
- C# 多线程的阻塞和继续-ManaulResetEvent的使用
在工作中,会遇到需要多线程处理相应的业务需求,最典型的包括Socket的通信. 多线程处理里,就会考虑到,哪个线程先运行,哪个线程后运行的情况. 这里我介绍一下,使用ManualResetEvent类 ...
- GitHub当作私密的版本控制系统远端版本库私有化
目的 我打算把所有服务器的配置文件用git管理起来,这样可以记录配置变更状况. 但是有一个问题是,如何多人协作?服务器配置信息非常敏感,如果这个版本库泄漏,整个公司的服务器架构就彻底泄漏了. 这个版本 ...