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 ...
随机推荐
- [Algorithm] Deferred Acceptance Algorithm
约会配对问题 一.立即接受算法: 对于约会的配对,大家都去追自己最心仪的女生.而这个女生面对几位追求者,要立刻做个决定. 被拒绝的男生们调整一下心情,再去追求心中的 No. 2.以此类推. 这样做法有 ...
- PMP模拟考试-2
1. Increasing resources on the critical path activities may not always shorten the length of the pro ...
- SpringMVC -- 梗概--源码--贰--静态资源的访问问题
配置:<mvc:default-servlet-handler/> 1>静态资源:除了Servlet.Controller之外的资源,如:js,css,png,html等 2> ...
- Linux Eclipse 运行Protobuf
安装环境Ubuntu 14.04 64 bit 安装过程分为三步 1. Linux下安装Protobuf 2. Eclipse下安装protobuf.dt插件 3. Eclipse下配置动态链接库并运 ...
- 如何在windows上测试iphone?
本教程将会让你没有mac照样测试iphone,这是我折腾了几天总结下来的,希望对大家有用. 先来几张效果图吧 方法很简单,但是配置起来说实话有点麻烦,先在电脑上安装vmware,在安装osx系统,在安 ...
- ios开发之--使用AFN上传3.1.0上传视频,不走成功回调原因及解决方法
在测试接口的时候,发现接口称走走了,但是success的回调不走,检查了下代码,发现没有初始化下面两个方法: manage.responseSerializer = [AFHTTPResponseSe ...
- 【Cesium】flyTo
// 1. Fly to a position with a top-down view viewer.camera.flyTo({ destination : Cesium.Cartesian3.f ...
- WAF Bypass数据库特性(MSsql探索篇)
0x01 背景 探索玩了Mysql特性,继续来探索一下MSsql特性. 0x02 测试 常见有5个位置即:select * from admin where id=1[位置一]union[位置二]se ...
- 使用librtmp进行H264与AAC直播
libx264 版本是 128libfaac 版本是 1.28 1.帧的划分 1.1 H.264 帧 对于 H.264 而言每帧的界定符为 00 00 00 01 或者 00 00 01. 比如下面的 ...
- Kafka manager安装 (支持0.10以后版本consumer)
下载地址: https://pan.baidu.com/s/1jIE3YL4 步骤: 1. 解压kafka-manager-1.3.2.1.zip 2. cd kafka-manager-1.3.2 ...