POJ-1458 Common Subsequence(线性动规,最长公共子序列问题)
Common Subsequence
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 44464 Accepted: 18186
Description
A subsequence of a given sequence is the given sequence with some elements (possible none) left out. Given a sequence X = < x1, x2, …, xm > another sequence Z = < z1, z2, …, zk > is a subsequence of X if there exists a strictly increasing 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.
Input
The program input is from the std input. Each data set in the input contains two strings representing the given sequences. The sequences are separated by any number of white spaces. The input data are correct.
Output
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.
Sample Input
abcfbc abfcab
programming contest
abcd mnp
Sample Output
4
2
0
裸的最长公共子序列问题:
状态转移方程:
if(s1[i]==s2[j])
dp[i][j]=dp[i-1][j-1]+1;
else
dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
#include <iostream>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <stdlib.h>
#include <string>
using namespace std;
char s1[300];
char s2[300];
int dp[300][300];
int main()
{
while(scanf("%s%s",&s1,&s2)!=EOF)
{
int len1=strlen(s1);
int len2=strlen(s2);
memset(dp,0,sizeof(dp));
for(int i=0;i<len1;i++)
{
for(int j=0;j<len2;j++)
{
if(s1[i]==s2[j])
dp[i+1][j+1]=dp[i][j]+1;
else
dp[i+1][j+1]=max(dp[i][j+1],dp[i+1][j]);
}
}
cout<<dp[len1][len2]<<endl;
}
return 0;
}
POJ-1458 Common Subsequence(线性动规,最长公共子序列问题)的更多相关文章
- HDU 1159 Common Subsequence (动规+最长公共子序列)
Common Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- HDU 1159 Common Subsequence (动态规划、最长公共子序列)
Common Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- HDU 1159 Common Subsequence --- DP入门之最长公共子序列
题目链接 基础的最长公共子序列 #include <bits/stdc++.h> using namespace std; ; char c[maxn],d[maxn]; int dp[m ...
- HDU 1159 Common Subsequence:LCS(最长公共子序列)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159 题意: 求最长公共子序列. 题解: (LCS模板题) 表示状态: dp[i][j] = max ...
- LCS POJ 1458 Common Subsequence
题目传送门 题意:输出两字符串的最长公共子序列长度 分析:LCS(Longest Common Subsequence)裸题.状态转移方程:dp[i+1][j+1] = dp[i][j] + 1; ( ...
- POJ 1458 Common Subsequence(LCS最长公共子序列)
POJ 1458 Common Subsequence(LCS最长公共子序列)解题报告 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?c ...
- poj 1458 Common Subsequence(区间dp)
题目链接:http://poj.org/problem?id=1458 思路分析:经典的最长公共子序列问题(longest-common-subsequence proble),使用动态规划解题. 1 ...
- (线性dp,LCS) POJ 1458 Common Subsequence
Common Subsequence Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 65333 Accepted: 27 ...
- POJ 1458 Common Subsequence(最长公共子序列LCS)
POJ1458 Common Subsequence(最长公共子序列LCS) http://poj.org/problem?id=1458 题意: 给你两个字符串, 要你求出两个字符串的最长公共子序列 ...
- poj 1458 Common Subsequence
Common Subsequence Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 46387 Accepted: 19 ...
随机推荐
- Eclipse------用Tomcat运行项目后出现:严重: Error configuring application listener of class org.springframework.web.context.ContextLoaderListener
Eclipse中Tomcat运行项目后出现: 严重: Error configuring application listener of class org.springframework.web.c ...
- 04-vi使用方法详细介绍
vi使用方法详细介绍 vi编辑器是所有Unix及Linux系统下标准的编辑器,它的强大不逊色于任何最新的文本编辑器,这里只是简单地介绍一下它的用法和一小部分指令.由于对Unix及Linux系统的任何版 ...
- vs2012修复问题
多装了一个.net framework4.5.1结果vs不能拥,借用了下面这个工具将vs2012从注册表中删除了 就能重装了 http://www.auslogics.com/en/software/ ...
- ios开发之NSString用strong还是用copy?
代码如下: 1,声明 @property(nonatomic,strong)NSString *firstName; @property(nonatomic,copy)NSString *second ...
- android 网络检测
这个过程我觉得有必要记录一下事情的起因是这样的, 写的程序在虚拟机下面无法连接到服务器,首先想到的是,虚拟机能不能访问外网,打开某搜索网站,正常,想用ping命令来ping服务器,于是就有了下面的过程 ...
- PostgreSQL主备切换
备库如何激活 在PostgreSQL(HOT-Standby)如主库出现异常.备库如何激活:来替换主库工作.有下列2种方式 备库在recovery.conf文件中有个配置项trigger_file.它 ...
- head first python 支持网站(可下载所有的代码和示例)
http://examples.oreilly.com/0636920003434/
- EhCache初体验
一.简介 EhCache 是一个纯Java的进程内缓存框架,具有快速.精干等特点.Ehcache是一种广泛使用的开源Java分布式缓存.主要面向通用缓存,Java EE和轻量级容器.它具有内存和磁盘存 ...
- Nginx(六)-- 配置文件之Gzip
1.概念及作用 Gizp主要对内容.静态文件做压缩,用来提升网站访问速度,节省带宽. 2.使用方法 gzip既可以配置在server中,也可以配置在server外,此处配置在server中,如下: ...
- MQTT-C-UDP_PUB
/** ****************************************************************************** * @file apdu.c ...