公共子序列 LCS 动态规划

求两序列的最大公共子序列

 

1,递归法

 

/*

 * Author: dengzhaoqun

 * Date   : 2011/04/28

 */

 

 

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

 

#define LENGTH 1000

 

// global variables

char X[1000]={'/0'};

char Y[1000]={'/0'};

int *C=NULL;

char *B=NULL;

 

// function declaration

int *BuildC(int lenx,int leny);

char *BuildB(int lenx,int leny);

int LCRRecur(int endx,int endy,int leny);

void Trace(int endx,int endy,int leny);

int main()

{

int lenx;

int leny;

int lcrn;  // LCR number

 

// get the input

printf("Input sequence X: ");

scanf("%s",&X[1]);  // endx decrease from lenx to 1

X[0]='T'  ;  // make the lenx not be 0

 

printf("Input sequence Y: ");

scanf("%s",&Y[1]);  // endy decrease from leny to 1

Y[0]='F' ;  // make the leny not be 0

 

lenx=strlen(X)-1;

leny=strlen(Y)-1;

 

// built the C[] and B[]

C=BuildC(lenx,leny);

B=BuildB(lenx,leny);

 

// call the recurvise function

        lcrn=LCRRecur(lenx,leny,leny);  // endx,endy,leny

 

// output

printf("The LCR number is: %d/n",lcrn);

printf("The LCR: ");

Trace(lenx,leny,leny);

printf("/n");

 

// release the memory

free(C);

free(B);

return 0;

}

 

// --- begin method BuildC ---

int *BuildC(int lenx,int leny)

{

int *p=NULL;

int i,j;

 

p=(int *)malloc((lenx+1)*(leny+1)*sizeof(int));

if(NULL==p)

{

printf("Memory allocation failed./n");

exit(1);

}

 

i=0;

for(i;i<=lenx;i++)

{

j=0;

for(j;j<=leny;j++)

{

p[i*(leny+1)+j]=-1;

}

}

for(i=0;i<=lenx;i++)

p[i*(leny+1)]=0;

for(i=0;i<=leny;i++)

p[i]=0;

 

return p;

}

// --- end method BuildC ---

 

// --- begin method BuildB ---

char *BuildB(int lenx,int leny)

{

char *ch=NULL;

int i,j;

 

ch=(char *)malloc((lenx+1)*(leny+1)*sizeof(char));

if(NULL==ch)

{

printf("Memory allocation failed./n");

exit(1);

}

 

i=0;

for(i;i<=lenx;i++)

{

j=0;

for(j;j<=leny;j++)

{

ch[i*(leny+1)+j]='E';  // indicate Error

}

}

return ch;

}

// --- end method BuildB ---

 

// --- begin method LCRRecur ---

int LCRRecur(int endx,int endy,int leny)

{

int tempx,tempy;

 

int *this=&(C[endx*(leny+1)+endy]);

if(*this!=-1)

return (*this);

else if(X[endx]==Y[endy])

{

*this=LCRRecur(endx-1,endy-1,leny)+1;

B[endx*(leny+1)+endy]='B';  // 'B' represents 'Both'

}

else

{

tempx=LCRRecur(endx-1,endy,leny);

tempy=LCRRecur(endx,endy-1,leny);

if(tempx>tempy)

{

*this=tempx;

B[endx*(leny+1)+endy]='X';  // decrease the endx

}

else

{

*this=tempy;

B[endx*(leny+1)+endy]='Y'; // decrease the endy

}

}

return *this;

}

// --- end method LCRRecur ---

 

// --- begin method Trace ---

void Trace(endx,endy,leny)

{

char *this=&(B[endx*(leny+1)+endy]);

if((endx==0)||(endy==0))

return;

if(*this=='B')

{

Trace(endx-1,endy-1,leny);

printf("%c",X[endx]);

}

else if(*this=='X')

{

Trace(endx-1,endy,leny);

}

else if(*this=='Y')

{

Trace(endx,endy-1,leny);

}

else if(*this=='E')

{

printf("Error./n");

exit(1);

}

}

// --- end method Trace ---

2, 迭代法

/*

 * Author: dengzhaoqun

 * Date   : 2011/04/28

 */

 

 

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

 

#define LENGTH 1000

 

// global variables

char X[1000]={'/0'};

char Y[1000]={'/0'};

int *C=NULL;

char *B=NULL;

 

// function declaration

int *BuildC(int lenx,int leny);

char *BuildB(int lenx,int leny);

void LCRIterator(int endx,int endy,int leny);

void Trace(int endx,int endy,int leny);

int main()

{

int lenx;

int leny;

 

// get the input

printf("Input sequence X: ");

scanf("%s",&X[1]);  // endx decrease from lenx to 1

X[0]='T'  ;  // make the lenx not be 0

 

printf("Input sequence Y: ");

scanf("%s",&Y[1]);  // endy decrease from leny to 1

Y[0]='F' ;  // make the leny not be 0

 

lenx=strlen(X)-1;

leny=strlen(Y)-1;

 

// built the C[] and B[]

C=BuildC(lenx,leny);

B=BuildB(lenx,leny);

 

// call the recurvise function

LCRIterator(lenx,leny,leny);

 

// output

printf("The LCR number is: %d/n",C[(lenx+1)*(leny+1)-1]);

printf("The LCR: ");

Trace(lenx,leny,leny);

printf("/n");

 

// release the memory

free(C);

free(B);

return 0;

}

 

// --- begin method BuildC ---

int *BuildC(int lenx,int leny)

{

int *p=NULL;

int i,j;

 

p=(int *)malloc((lenx+1)*(leny+1)*sizeof(int));

if(NULL==p)

{

printf("Memory allocation failed./n");

exit(1);

}

 

i=0;

for(i;i<=lenx;i++)

{

j=0;

for(j;j<=leny;j++)

{

p[i*(leny+1)+j]=-1;

}

}

for(i=0;i<=lenx;i++)

p[i*(leny+1)]=0;

for(i=0;i<=leny;i++)

p[i]=0;

 

return p;

}

// --- end method BuildC ---

 

// --- begin method BuildB ---

char *BuildB(int lenx,int leny)

{

char *ch=NULL;

int i,j;

 

ch=(char *)malloc((lenx+1)*(leny+1)*sizeof(char));

if(NULL==ch)

{

printf("Memory allocation failed./n");

exit(1);

}

 

i=0;

for(i;i<=lenx;i++)

{

j=0;

for(j;j<=leny;j++)

{

ch[i*(leny+1)+j]='E';  // indicate Error

}

}

return ch;

}

// --- end method BuildB ---

 

// --- begin method LCRRecur ---

void LCRIterator(int endx,int endy,int leny)

{

int i,j;

int tempx,tempy;

int *this=NULL;

for(i=1;i<=endx;i++)

for(j=1;j<=endy;j++)

{

this=&(C[i*(leny+1)+j]);

if(X[i]==Y[j])

{

*this=C[(i-1)*(leny+1)+(j-1)]+1;

B[i*(leny+1)+j]='B' ;  // 'B' represent 'Both'

}

else

{

        tempx=C[(i-1)*(leny+1)+j];

       tempy=C[i*(leny+1)+(j-1)];

if(tempx>tempy)

{

*this=tempx;

B[i*(leny+1)+j]='X';

}

else

{

*this=tempy;

B[i*(leny+1)+j]='Y';

}

}

}

}

// --- end method LCRRecur ---

 

// --- begin method Trace ---

void Trace(endx,endy,leny)

{

char *this=&(B[endx*(leny+1)+endy]);

if((endx==0)||(endy==0))

return;

if(*this=='B')

{

Trace(endx-1,endy-1,leny);

printf("%c",X[endx]);

}

else if(*this=='X')

{

Trace(endx-1,endy,leny);

}

else if(*this=='Y')

{

Trace(endx,endy-1,leny);

}

else if(*this=='E')

{

printf("Error./n");

exit(1);

}

}

// --- end method Trace ---

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值