D - Common Subsequence

Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d
& %I64u

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, 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

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
一定要用记忆化,要不然会超时,还有刚开始数组开的太小了,只开到一百,结果是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的更多相关文章

  1. 动态规划求最长公共子序列(Longest Common Subsequence, LCS)

    1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...

  2. LintCode Longest Common Subsequence

    原题链接在这里:http://www.lintcode.com/en/problem/longest-common-subsequence/ 题目: Given two strings, find t ...

  3. [UCSD白板题] Longest Common Subsequence of Three Sequences

    Problem Introduction In this problem, your goal is to compute the length of a longest common subsequ ...

  4. LCS(Longest Common Subsequence 最长公共子序列)

    最长公共子序列 英文缩写为LCS(Longest Common Subsequence).其定义是,一个序列 S ,如果分别是两个或多个已知序列的子序列,且是所有符合此条件序列中最长的,则 S 称为已 ...

  5. Longest Common Subsequence

    Given two strings, find the longest common subsequence (LCS). Your code should return the length of  ...

  6. LCS POJ 1458 Common Subsequence

    题目传送门 题意:输出两字符串的最长公共子序列长度 分析:LCS(Longest Common Subsequence)裸题.状态转移方程:dp[i+1][j+1] = dp[i][j] + 1; ( ...

  7. Common Subsequence LCS

    题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87730#problem/F 题目: Description A subsequ ...

  8. poj 1458 Common Subsequence

    Common Subsequence Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 46387   Accepted: 19 ...

  9. Longest Increasing Common Subsequence (LICS)

    最长上升公共子序列(Longest Increasing Common Subsequence,LICS)也是经典DP问题,是LCS与LIS的混合. Problem 求数列 a[1..n], b[1. ...

  10. Common Subsequence(dp)

    Common Subsequence Time Limit: 2 Sec  Memory Limit: 64 MBSubmit: 951  Solved: 374 Description A subs ...

随机推荐

  1. 【翻译自mos文章】改变数据库用户sysman(该用户是DB Control Repository 的schema)password的方法

    改变数据库用户sysman(该用户是DB Control Repository 的schema)password的方法 參考原文: How To Change the Password of the ...

  2. 使用Vitamio打造自己的Android万能播放器(4)——本地播放(快捷搜索、数据存储)

    前言 关键字:Vitamio.VPlayer.Android播放器.Android影音.Android开源播放器 本章节把Android万能播放器本地播放的主要功能(缓存播放列表和A-Z快速查询功能) ...

  3. oracle实现今年数据 去年同期和增长百分比

    select c.*,round((datanow-databefore)/databefore,2)*100||'%' datapercent from (select a.auth_tztype ...

  4. Java异常分类 转载

    Java异常分类 http://blog.csdn.net/woshixuye/article/details/8230407     一.基本概念 看java的异常结构图 Throwable是所有异 ...

  5. CCF计算机认证注意事项

    1,同一变量只使用一次,你是使用同名的局部变量. 2,if()条件语句里面再不要使用单一的if()条件语句. 这应该都是他们系统的bug

  6. 相见恨晚——MarkDown

    什么是MarkDown MarkDown是一种轻量级的标记语言 MarkDown使你更加关注文章的内容 MarkDown使文章的排版变得简单直接 什么情景下使用MarkDown 在我们熟悉的githu ...

  7. Linux学习之traceroute命令

    通过traceroute我们可以知道信息从你的计算机到互联网另一端的主机是走的什么路径.当然每次数据包由某一同样的出发点(source)到达某一同样的目的地(destination)走的路径可能会不一 ...

  8. 生成php所需要的APNS Service pem证书的步骤

    1.登录到 iPhone Developer Connection Portal 并点击 App IDs 2.创建一个不使用通配符的 App ID .通配符 ID 不能用于推送通知服务.例如,我们的i ...

  9. CentOS 6下配置本地用户访问vsftpd并赋予写权限

    一.安装并测试可用性 1.安装命令 yum install vsftpd 2.配置防火墙,加入一行 -A INPUT -p tcp -m tcp --dport 21 -j ACCEPT 在其它机测试 ...

  10. 方形图片转动并转换成圆形CSS特效

    <style> img { transition:all 0.8s ease 0s;} img:hover { border-radius:50%; transform:rotate(72 ...