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 ...
随机推荐
- startActivityForResult不返回结果
startActivityForResult不返回结果,请检查AndroidManifest中的描写叙述,是否对该Activity设置了:launchMode="singleTask&quo ...
- DOM元素对象的属性和方法(1)
一.accessKey() 作用:获取元素焦点快捷键:设置快捷键后,使用Alt+快捷键,让元素快速获得焦点, <!DOCTYPE html> <html> <head&g ...
- C#扩展方法的理解 (转)
“扩展方法使您能够向现有类型“添加”方法,而无需创建新的派生类型.重新编译或以其他方式修改原始类型.” 这是msdn上说的,也就是你可以对String,Int,DataRow,DataTable等这些 ...
- 网络分析shell脚本(实时流量+连接统计)
介绍一个强大的分析网络的shell脚本,此脚本是从EZHTTP拆分出来的,觉得有必要单独介绍下.脚本运行效果截图: 此脚本包含的功能有: 1.实时监控任意网卡的流量 2.统计10秒内平均流量 3.统计 ...
- 众数问题(为什么只能输入一组数据,不能输入m组数据)
描述 所谓众数,就是对于给定的含有N个元素的多重集合,每个元素在S中出现次数最多的成为该元素的重数, 多重集合S重的重数最大的元素成为众数.例如:S={1,2,2,2,3,5},则多重集S的众数是2, ...
- mysql 存储过程中的declare 和 set @的两种变量的区别
两者在手册中的说明: DECLARE var_name[,...] type [DEFAULT value]这个语句被用来声明局部变量.要给变量提供一个默认值,请包含一个DEFAULT子句.值可以被指 ...
- Java 7源码分析第13篇 - 字节输入输出流(1)
上一篇介绍了关于字节输入输出流的Java类框架,同时也简单介绍了一下各个类的作用,下面就来具体看一下这些类是怎么实现这些功能的. 1.InputStream和OutputStream InputStr ...
- Android studio 查看sha1
高德地图开发申请KEY的时候需要开发者提供SHA1证书指纹数据,在eclipse很容易就找到了,但是Android Studio很久也没找到,只能使用在网上看到的方法了,在Android Studio ...
- 编写一个程序实现strcpy函数的功能
#include <stdio.h> #include <string.h> #define N 5 char *mycpy(char *s1, char *s2) { //数 ...
- MySQLdb autocommit
MySQLdb 中 autocommit 默认是关闭的,下面是例子. import MySQLdb conn = MySQLdb.connect(host='127.0.0.1',user='root ...