hdu 1159 - CommonSubsequence

9 篇文章 0 订阅

1159 - CommonSubsequence

201442

19:20

Common Subsequence

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 872 Accepted Submission(s): 473

Problem 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, xij = 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. 

The program input is from a text file. Each data set in the file contains two strings representing the given sequences. The sequences are separated by any number of white spaces. The input data are correct. 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

 

 

来自 <http://acm.hdu.edu.cn/game/entry/problem/show.php?chapterid=3&sectionid=2&problemid=2>

 

传说这是一道动态规划入门的题目TT

题目大意:一个子序列是由另一个序列除去一些元素所构成。求x序列和Y序列的最大共同子序列。

例如:x=<a,b,f,c>y=<a,b,c,f,b,c>中,y1,2,4,6元素构成<a,b,f,c>序列与x的子序列最大为4.

给定两个字符串序列,求它们的最大共同子串的个数。

刚开始接触这道题毫无头绪和思路,网上看他们博客是一个入门的dp问题而后他们给出一张表格

(借用下表格,不算侵权吧 ~~


 

说是由图得如果x[i]y[i]匹配到,则dp[i][j] = dp[i-1][j-1]+1。然后后面的全部更新为前面那个

如果匹配不到,dp[i][j]= max(dp[i-1][j],dp[i][j-1])。按照图表一开始我开了int[1000][1000]的数组

,然后匹配到时就令dp[i][j]= dp[i-1][j-1]+1,然后x[i]后面跟前面对比,留下大的数值,结果wa

于是我开始分析,发现啊少了x[i]多次匹配的情况,再结合图表分析dp,发现并不用开多大的数组!

 

dp算法分析:如图所示想要知道abfabcfbc的最大共同子串,当f在第4个匹配上时,只需知道ab

3位(abc)上的最大共同子串+1就可以了,再看bb在第25都有匹配,b在第2位(ab)最大子串

a在第1位的最大子串,b在第5位看a在到第4位前的最大匹配,也就是说当前匹配的最大子串可由

前一匹配的最大子串得出,所以只需要两个数组而不用1000组存储!

 

代码如下:

#include<iostream>

#include<string>

using namespace std;

const int howNum =1000;                                      //数组大小

int xyMax[howNum],xyPerMax[howNum];              //当前和前一个数组最大子串的存储

int*theMax = xyMax,*perMax =xyPerMax;        //指针用于下面把当前数组变为前一个数组,免去赋值

charxString[howNum],yString[howNum];                //存储用户输入的数组

int main(){

while(scanf("%s%s",xString,yString) != EOF){

memset(xyPerMax,0,sizeof(xyPerMax));

memset(xyMax,0,sizeof(xyMax));

intxEnd = 0,yEnd = 0;

xEnd= strlen(xString);

yEnd= strlen(yString);

for(intj = 0; j < yEnd; j++){

if(xString[0]== yString[j]) {

perMax[j]++;

while(j < yEnd) perMax[++j] = perMax[j-1];

}

}

for(inti = 1; i < xEnd; i++){

for(intj = 0; j < yEnd; j++){

if(xString[i]!= yString[j])  {

if(j== 0) theMax[j] = perMax[j];

theMax[j]= theMax[j-1] < perMax[j] ? perMax[j] : theMax[j-1];

}

else{

if(j== 0) theMax[j] = 1;

theMax[j]= perMax[j-1] + 1;

}

}

int*temp = perMax;

perMax= theMax;

theMax= temp;

}

cout<< perMax[yEnd-1] << endl;

}

}

下面是提交结果,表示那些4000+内存的不解释,当然,很多人只是练下算法,只是我太较真罢了~~


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值