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. hdu 2768

    求最大留下的观众,观众之间存在不能同时满足的关系,就是矛盾关系, 矛盾关系建边,建边是双向的所以最大匹配要/2 还有一种建图的方法:把观众分成两个集合,一个是投留下猫的,一个是投留下狗的 每个集合间没 ...

  2. XCode常用快捷键(转)

    刚开始用Xcode是不是发现以前熟悉的开发环境的快捷键都不能用了?怎么快捷运行,停止,编辑等等.都不一样了.快速的掌握这些快捷键,能提供开发的效率. 其实快捷键在Xcode的工具栏里都标注有,只是有的 ...

  3. 根据自己的需要适度使用Web开发框架

    软件系统发展到今天已经很复杂了,特别是服务器端软件,涉及到的知识,内容,问题太多.Web开发框架能够帮我们大大减少工作量,但是我们应该如何正确看待Web开发框架,并且如何去使用他们呢? 对框架的依赖 ...

  4. 第四章SignalR自托管主机

    第四章SignalR自托管主机 SignalR服务器通常在IIS的Asp.Net应用程序上承载,但它也可以使用自托管库来作为自托管的主机来运行(就像控制台应用程序或Windows服务那样)与Signa ...

  5. Gulp插件汇总

    HTML&CSS autoprefixer - parse CSS and add vendor prefixes to rules by Can I Use. gulp-browser-sy ...

  6. iis7.0/8.0rewrite规则

    首先在网站 web.config 配置文件的 configuration 节点中加入如下代码: <system.webServer> <rewrite> <rules&g ...

  7. ScrollView属性fillViewport解决android布局不能撑满全屏的问题

    转:http://blog.sina.com.cn/s/blog_6cf2ea6a0102v61f.html 开发项目中遇到一个问题,布局高度在某些国产酷派小屏幕手机上高度不够全部显示,于是使用了Sc ...

  8. placeholder 兼容 IE

    placeholder 是 html5 的新属性,仅支持 html5 的浏览器才支持 placeholder,目前最新的 FF.Chrome.Safari.Opera 以及 IE10 都支持,IE6- ...

  9. [LeetCode]题解(python):148-Sort List

    题目来源: https://leetcode.com/problems/sort-list/ 题意分析: 用nlog(n)的时间复杂度实现一个链表的排序. 题目思路: 用归并排序的思想,将链表用快慢指 ...

  10. 一个SysLog实现

    http://www.ice.com/java/syslog/index.shtml http://alvinalexander.com/servlets