D - Common Subsequence
Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d
& %I64u
Description
i2, ..., ik > of indices of X such that for all j = 1,2,...,k, x ij = 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
一定要用记忆化,要不然会超时,还有刚开始数组开的太小了,只开到一百,结果是RE。改到600后就好了。
my answer :
一、记忆化了的。
#include<iostream>
#include<stdio.h>
#include<string>
#include<cstring>
using namespace std;
int main()
{
char a[600],b[600];
while(scanf("%s%s",a,b)!=EOF)
{
int t1=strlen(a);
int t2=strlen(b);
int dp[600][600];
memset(dp,-1,sizeof(dp));
for(int i=t1;i>=0;i--)
a[i+1]=a[i];
for(int j=t2;j>=0;j--)
b[j+1]=b[j];
for(int i=0;i<=t1;i++){
for(int j=0;j<=t2;j++){
if(i==0||j==0)dp[i][j]=0;
else if(a[i]==b[j]&&dp[i][j]<0){dp[i][j]=dp[i-1][j-1]+1;}
else if(dp[i][j]<0){dp[i][j]=max(dp[i-1][j],dp[i][j-1]);} }
}
printf("%d\n",dp[t1][t2]);
}
return 0;
}别人写的:进行了空间的优化:<pre name="code" class="cpp">#include <stdio.h>
#include <string.h>
char s1[1001], s2[1001];
int dp[1001], t, old, tmp;
int main(){
scanf("%d", &t);
getchar();
while(t--){
gets(s1);
gets(s2);
memset(dp, 0, sizeof(dp));
int lenS1=strlen(s1), lenS2=strlen(s2);
for(int i=0; i<lenS1; i++){//若s1[i]==s2[j], dp[i][j] = dp[i-1][j-1]+1 否则,dp[i][j] = max(dp[i-1][j], dp[i][j-1])
old=0;//此处进行了空间优化,old 代表 dp[i-1][j-1] dp[j-1] 代表 dp[i][j-1], dp[j] 代表 dp[i-1][j]
for(int j=0; j<lenS2; j++){
tmp = dp[j];
if(s1[i]==s2[j])
dp[j] = old+1;
else
if(dp[j-1]>dp[j])dp[j]=dp[j-1];
old = tmp;
}
}
printf("%d\n", dp[lenS2-1]);
}
return 0;
}
写的太烂,下面是学姐的代码:
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<iostream>
using namespace std;
#define max_n 1000
#define max_m 1000
int dp[max_n][max_m];
char s[max_n],t[max_m];
int main()
{
while(scanf("%s%s",s,t)!=EOF)
{
int n=strlen(s);
int m=strlen(t);
memset(dp,0,sizeof(dp));
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(s[i]==t[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]);
}
}
printf("%d\n",dp[n][m]);
}
return 0;
}再写一个:
试试即记忆化,又空间优化一下:等一会吧。。。。。。让我想想。。。
D - Common Subsequence的更多相关文章
- 动态规划求最长公共子序列(Longest Common Subsequence, LCS)
1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...
- LintCode Longest Common Subsequence
原题链接在这里:http://www.lintcode.com/en/problem/longest-common-subsequence/ 题目: Given two strings, find t ...
- [UCSD白板题] Longest Common Subsequence of Three Sequences
Problem Introduction In this problem, your goal is to compute the length of a longest common subsequ ...
- LCS(Longest Common Subsequence 最长公共子序列)
最长公共子序列 英文缩写为LCS(Longest Common Subsequence).其定义是,一个序列 S ,如果分别是两个或多个已知序列的子序列,且是所有符合此条件序列中最长的,则 S 称为已 ...
- Longest Common Subsequence
Given two strings, find the longest common subsequence (LCS). Your code should return the length of ...
- LCS POJ 1458 Common Subsequence
题目传送门 题意:输出两字符串的最长公共子序列长度 分析:LCS(Longest Common Subsequence)裸题.状态转移方程:dp[i+1][j+1] = dp[i][j] + 1; ( ...
- Common Subsequence LCS
题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87730#problem/F 题目: Description A subsequ ...
- poj 1458 Common Subsequence
Common Subsequence Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 46387 Accepted: 19 ...
- Longest Increasing Common Subsequence (LICS)
最长上升公共子序列(Longest Increasing Common Subsequence,LICS)也是经典DP问题,是LCS与LIS的混合. Problem 求数列 a[1..n], b[1. ...
- Common Subsequence(dp)
Common Subsequence Time Limit: 2 Sec Memory Limit: 64 MBSubmit: 951 Solved: 374 Description A subs ...
随机推荐
- Unity CCTween UGUI 动画插件
在这简单的介绍一下 CCTween 动画插件的使用 因为GIF 制作软件不太好(网上随便下载的)所以导致效果不太好,有时间我重新制作一下 这是一下简单的效果 下面介绍怎么使用 首先 先下载 CCTwe ...
- ASP.NET 验证码 不同浏览器 不刷新问题
具体为什么不刷新是缓存机制不同,验证码图片的src或ImageUrl的获取是来自一个文件,由于连接地址没变所以不同内核浏览器有的会认为源没有变,解决办法就是在连接后面加上一个随机参数如可以用JS的Ma ...
- 谈谈css3的字体大小单位[rem]
最近接收了一份面试题,内容是移动端传播的H5(在中国通常这么叫)广告页. 秉承移动端web尽量少用px的概念,我使用rem进行了一次重构.对于rem,基本是给 html/body 元素定义一个字体大小 ...
- OC中文件的操作
OC中文件操作,在之前的文章中,已经接触到了文件的创建了,但是那不是很具体和详细,这篇文章我们就来仔细看一下OC中是如何操作文件的: 第一.首先来看一下本身NSString类给我们提供了哪些可以操作文 ...
- Spring事务异常回滚,捕获异常不抛出就不会回滚(转载) 解决了我一年前的问题
最近遇到了事务不回滚的情况,我还考虑说JPA的事务有bug? 我想多了....... 为了打印清楚日志,很多方法我都加tyr catch,在catch中打印日志.但是这边情况来了,当这个方法异常 ...
- lightoj 1079 Just another Robbery
题意:给出银行的个数和被抓概率上限.在给出每个银行的钱和抢劫这个银行被抓的概率.求不超过被抓概率上线能抢劫到最多的钱. dp题,转移方程 dp[i][j] = min(dp[i-1][j] , dp[ ...
- QF——OC中的SEL类型和Block
@selector(): 可以理解@selector()就是取类方法的编号,他的基本行为类似于C语言中的函数指针(指向函数的指针).它们通过传递方法的地址(或编号)来实现把方法当做参数的效果. 不过在 ...
- python 装饰器、内部函数、闭包简单理解
python内部函数.闭包共同之处在于都是以函数作为参数传递到函数,不同之处在于返回与调用有所区别. 1.python内部函数 python内部函数示例: def test(*args): def a ...
- J2SE知识点摘记(二十)
List 1.3.1 概述 前面我们讲述的Collection接口实际上并没有直接的实现类.而List是容器的一种,表示列表的意思.当我们不知道存储的数据有多少的情况,我们就可以使用Li ...
- (8) Xamarin使用Jar檔
原文 Xamarin使用Jar檔 这个范例是如何在Xamarin.Android中去使用一个我们自行在开发的JAR档案. 主要会执行的步骤如下 在Xamarin建立一个Android Java Bin ...