LCS源代码(C语言)

LCS算法原理参见:http://blog.csdn.net/v_july_v/article/details/6695482



#include <stdlib.h>
#include <stdio.h>
#include <string.h>


#define LEFT  	2
#define UP	 	3
#define LR		4


int LCS(char *str1, char *str2, int len1, int len2, char *b, char *c)
{
	if(NULL == str1 || NULL == str2 || NULL == b || NULL == c){
		return -1;
	}

	int i, j;

	for(i = 0; i <= len1; i++){
		*(b + len1 * i) = 0; //b[i][j] = *(*b + i * len1+ j)
		*(c + len1 * i) = 0;
	}

	for(j = 0; j <= len2; j++){
		*(b + j) = 0;
		*(c + j) = 0;
	}

	for(i = 1; i <= len1; i++){
		for(j = 1; j <= len2; j++){
			if(str1[i-1] == str2[j-1]){
				*(c + i * len1+ j) = *(c + (i - 1) * len1 + j - 1) + 1;
				*(b + i * len1+ j) = LR;
			}else{
				if(*(c + (i - 1) * len1 + j) >= *(c + i * len1 + j - 1)){
					*(c + i * len1 + j) = *(c + (i - 1) * len1 + j);
					*(b + i * len1 + j) = UP;
				}else{
					*(c + i * len1 + j) = *(c + i * len1 + j - 1);
					*(b + i * len1 + j) = LEFT;
				}
			}
		}
	}

	return 0;
}

int PrintLCS(char *b, char *str1, int len1, int len2)
{
	if(0 == len1 || 0 == len2 || NULL == b || NULL == str1){
		return -1;
    }


	if(*(b + len1 * 7 + len2) == LR){
		PrintLCS(b, str1, len1 - 1, len2 - 1);
		printf("%c ", str1[len1 - 1]);
	}else{
        if(*(b + len1 * 7 + len2) == UP){
			PrintLCS(b, str1, len1 - 1, len2);
        }else if(*(b + len1 * 7 + len2) == LEFT){
			PrintLCS(b, str1, len1, len2 - 1); 
		}
	}
	return 0;
}

int main(void)
{
	char *X = "ABCBDAB";
	char *Y = "BDCABA";


	int len1 = strlen(X);
	int len2 = strlen(Y);


	char b[len1+1][len2+1], c[len1+1][len2+1];


    int i,j;
    
	LCS(X,Y,len1,len2,*b,*c);
    
    for(i = 0; i <= len1; i++){
        for(j = 0; j <= len2; j++){
            printf("%d ", c[i][j]);
        }
        printf("\n");
    }
    printf("\n");
    for(i = 0; i <= len1; i++){
        for(j = 0; j <= len2; j++){
            if(b[i][j] == LR)
                printf("* ");
            else if(b[i][j] == LEFT)
                printf("< ");
            else if(b[i][j] == UP)
                printf("^ ");
            else
                printf("  ");
        }
        printf("\n");
    }
    printf("\n");

	PrintLCS(*b,X,len1,len2);

	return 0;

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值