POJ 1458 Common Subsequence (zoj 1733 ) LCS
POJ:http://poj.org/problem?id=1458
ZOJ:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=733
HDU: http://acm.hdu.edu.cn/showproblem.php?pid=1159
题目大意:
给定两串子序列,求最长的公共字串(LCS)
设d( i , j)为A和 B的LCS的长度,则当A[i] = B[j]时, d(i , j)= d(i-1, j-1)+1 ; 否则 d (i , j)=max ( d(i - 1 ,j) , d(i , j-1 )},时间复杂度为O(nm),n和m分别为序列A和B的长度。
可用滚动数组优化空间复杂度。
下面给出不用和用滚动数组的。
空间未用滚动数组优化版本。。。。
#include<cstdio>
#include<cstring>
const int MAXN=512;
char a[MAXN],b[MAXN];
int dp[MAXN][MAXN];
int main()
{
while(~scanf("%s%s",a+1,b+1))
{
memset(dp,0,sizeof(dp));
int len_a=strlen(a+1);
int len_b=strlen(b+1);
for(int i=1;i<=len_a;i++)
{
for(int j=1;j<=len_b;j++)
{
if(a[i] == b[j])
dp[i][j]= dp[i-1][j-1]+1;
else
dp[i][j]= dp[i][j-1] > dp[i-1][j]? dp[i][j-1] : dp[i-1][j];
}
}
printf("%d\n",dp[len_a][len_b]);
}
}
滚动数组优化空间:
设dp1为前一列,dp2为后面一列。
#include<cstdio>
#include<cstring>
const int MAXN=512;
char a[MAXN],b[MAXN];
int dp1[MAXN],dp2[MAXN];
int main()
{
while(~scanf("%s%s",a+1,b+1))
{
memset(dp1,0,sizeof(dp1));
memset(dp2,0,sizeof(dp2));
int len_a=strlen(a+1);
int len_b=strlen(b+1);
for(int i=1;i<=len_a;i++)
{ for(int j=1;j<=len_b;j++)
{
if(a[i] == b[j])
dp2[j]= dp1[j-1]+1;
else
dp2[j]= dp2[j-1] > dp1[j]? dp2[j-1] : dp1[j];
} memcpy(dp1,dp2,sizeof(dp2)); }
printf("%d\n",dp1[len_b]);
}
}
POJ 1458 Common Subsequence (zoj 1733 ) LCS的更多相关文章
- POJ 1458 Common Subsequence (DP+LCS,最长公共子序列)
题意:给定两个字符串,让你找出它们之间最长公共子序列(LCS)的长度. 析:很明显是个DP,就是LCS,一点都没变.设两个序列分别为,A1,A2,...和B1,B2..,d(i, j)表示两个字符串L ...
- 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(最长公共子序列LCS)
POJ1458 Common Subsequence(最长公共子序列LCS) http://poj.org/problem?id=1458 题意: 给你两个字符串, 要你求出两个字符串的最长公共子序列 ...
- Poj 1458 Common Subsequence(LCS)
一.Description A subsequence of a given sequence is the given sequence with some elements (possible n ...
- (线性dp,LCS) POJ 1458 Common Subsequence
Common Subsequence Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 65333 Accepted: 27 ...
- POJ - 1458 Common Subsequence DP最长公共子序列(LCS)
Common Subsequence A subsequence of a given sequence is the given sequence with some elements (possi ...
- poj 1458 Common Subsequence【LCS】
Common Subsequence Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 43132 Accepted: 17 ...
- OpenJudge/Poj 1458 Common Subsequence
1.链接地址: http://poj.org/problem?id=1458 http://bailian.openjudge.cn/practice/1458/ 2.题目: Common Subse ...
随机推荐
- learn ES6
介绍 ES6,也叫ECMAScript2015(以下统称ES6),是ECMAScript标准的最新版本.这个标准在2015年6月份被正式批准.ES6是js语言很有意义的一次更新,也是2009年ES5被 ...
- 【记录】无法读取配置节“AppSettings”,因为它缺少节声明
Web.config对大小写敏感, 把AppSettings改为appSettings即可.
- celery work logging 问题
celery 的日志里只输出日志 不输入标准打印
- Python爬虫之『urlopen』
本文以爬取百度首页为示例来学习,python版本为python3.6.7,完整代码会在文章末附上 本次学习所用到的python框架:urllib.request 本次学习所用到的函数: urllib. ...
- [Angular & Unit Testing] Automatic change detection
When you testing Component rendering, you often needs to call: fixture.detectChanges(); For example: ...
- Android线程池(二)——ThreadPoolExecutor及其拒绝策略RejectedExecutionHandler使用演示样例
MainActivity例如以下: package cc.vv; import java.util.concurrent.LinkedBlockingQueue; import java.util.c ...
- BestCoder Round #65 HDOJ5592 ZYB's Premutation(树状数组+二分)
ZYB's Premutation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Othe ...
- searchView-风格调整
5.1以后的searchView 风格调整属性相比于4.4有了些更改.我们先看代码 <style name="DeskClock.Theme" parent="an ...
- 16.REPL 命令
转自:http://www.runoob.com/nodejs/nodejs-tutorial.html ctrl + c - 退出当前终端. ctrl + c 按下两次 - 退出 Node REPL ...
- Android Okhttp完美同步持久Cookie实现免登录
通过对Retrofit2.0的<Retrofit 2.0 超能实践,完美支持Https传输>基础入门和案例实践,掌握了怎么样使用Retrofit访问网络,加入自定义header,包括加入S ...