POJ1458 Common Subsequence 【最长公共子序列】
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 37614 | Accepted: 15058 |
Description
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.
Input
Output
Sample Input
abcfbc abfcab
programming contest
abcd mnp
Sample Output
4
2
0
NYOJ同题
#include <stdio.h>
#include <string.h>
#define maxn 1000 char str1[maxn], str2[maxn];
int dp[maxn][maxn]; int max(int a, int b){ return a > b ? a : b; } int LCS()
{
int ans = 0;
for(int i = 1, j; str1[i]; ++i){
for(j = 1; str2[j]; ++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]);
if(dp[i][j] > ans) ans = dp[i][j];
}
}
return ans;
} int main()
{
while(scanf("%s%s", str1 + 1, str2 + 1) == 2){
printf("%d\n", LCS());
}
return 0;
}
POJ1458 Common Subsequence 【最长公共子序列】的更多相关文章
- poj1458 Common Subsequence ——最长公共子序列
link:http://poj.org/problem?id=1458 最基础的那种 #include <iostream> #include <cstdio> #includ ...
- POJ 1458 Common Subsequence(最长公共子序列LCS)
POJ1458 Common Subsequence(最长公共子序列LCS) http://poj.org/problem?id=1458 题意: 给你两个字符串, 要你求出两个字符串的最长公共子序列 ...
- C++版 - Lintcode 77-Longest Common Subsequence最长公共子序列(LCS) - 题解
版权声明:本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C++版 - L ...
- lintcode 77.Longest Common Subsequence(最长公共子序列)、79. Longest Common Substring(最长公共子串)
Longest Common Subsequence最长公共子序列: 每个dp位置表示的是第i.j个字母的最长公共子序列 class Solution { public: int findLength ...
- HDU 1159 Common Subsequence 最长公共子序列
HDU 1159 Common Subsequence 最长公共子序列 题意 给你两个字符串,求出这两个字符串的最长公共子序列,这里的子序列不一定是连续的,只要满足前后关系就可以. 解题思路 这个当然 ...
- LCS(Longest Common Subsequence 最长公共子序列)
最长公共子序列 英文缩写为LCS(Longest Common Subsequence).其定义是,一个序列 S ,如果分别是两个或多个已知序列的子序列,且是所有符合此条件序列中最长的,则 S 称为已 ...
- hdu 1159 Common Subsequence(最长公共子序列 DP)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159 Common Subsequence Time Limit: 2000/1000 MS (Jav ...
- 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)是一个在一个序列集合中(通常为两个序列)用来查找所有序列中最长子序列的问题.这与查找最长公共子串的问题不同的地方是:子序列不需要在原序列中占用连续的位置 .最长公共子序列问题是 ...
随机推荐
- 远程centos改动yum源
yum -y install unzip发现运行不了,说是找不到unzip的包,搜索发现时由于yum源的问题,那我就改动yum吧, 在网上找到的方法是这么说的: 1. cd /etc/yum.repo ...
- html 跳转页面,同时跳转到一个指定的位置
比如我现在 a.html 的时候,我想跳转到 b.html ,并且是 b.html 的某一个位置,用 <a href=>, a.html里: <a href="b.html ...
- 聊天demo SignalR
1首先这个demo是针对 net版本是4.5的 SignalR 获取的是2.2的 2新建一个mvc项目 3 Nuget 搜索 SignalR 安装如图的一项 4新建一个 集线器类 修改新 ...
- POJ 1205 Water Treatment Plants(递推)
题意 建设一条河岸的污水处理系统 河岸有n个城市 每一个城市都能够自己处理污水 V 也能够把污水传到相邻的城市处理 >或< 除了你传给我我也传给你这样的情况 其他都是 ...
- uva:10700 - Camel trading(贪婪)
题目:10700 - Camel trading 题目大意:给出一些表达式,表达式由数字和加号乘号组成,数字范围[1,20].这些表达式可能缺少了括号,问这种表达式加上括号后能得到的最大值和最小值. ...
- hdu 4524 郑厂长系列故事——逃离迷宫 小水题
郑厂长系列故事——逃离迷宫 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) To ...
- poj 2253 Frogger (最长路中的最短路)
链接:poj 2253 题意:给出青蛙A,B和若干石头的坐标,现青蛙A想到青蛙B那,A可通过随意石头到达B, 问从A到B多条路径中的最长边中的最短距离 分析:这题是最短路的变形,曾经求的是路径总长的最 ...
- 深度分析 Java 的枚举类型:枚举的线程安全性及序列化问题(转)
写在前面: Java SE5 提供了一种新的类型 Java的枚举类型,关键字 enum 可以将一组具名的值的有限集合创建为一种新的类型,而这些具名的值可以作为常规的程序组件使用,这是一种非常有用的功能 ...
- cidaemon.exe过程cpu入住率和关闭cidaemon.exe加工方法
问题叙述性说明: 这个时间机器始终是一个奇怪的问题:cidaemon.exe这个过程需要CUP率98%以上,大大影响了正常使用电脑.多个资源管理器出现cidaemon.exe过程,cpu率最高的一 ...
- testlink于smarty配置和使用
于testlink于,采用smarty首先配置. 一般在过程化的编程中.创建一个smarty.inc.php的文件来配置Smarty的信息,其它文件引入就可以,目的是为了不改动smarty.class ...