小白笔记--------------------构造最长公共子序列

/******************************************
*		Name:	 LCSLengh
*		Description:	Firgure out the longest same part of two
*		different alphabetic strings
*		Author:	Aaron92
*		Time:	2016/4/
*		Modified Time:	0
******************************************/

#include<stdio.h>
void LCSLength(char x[],char y[],int (*c)[4],int (*b)[4]);
void print1(char m[],int n);
void print2(int (*n)[4]);
void LCS(int i,int j ,char x[],int (*b)[4]);

void main(int argc, char ** argv){
	char String_A[5] = {'A','B','C','D','E'};/*input two diferent Strings*/
	char String_B[3] = {'D','E','F'};
	int c[6][4],b[6][4];/*c implies the length of the longest same string
						*d implies the situation*/
	int m,n;
	c[0][0] = 0;
	for(m = 0;m < 6;m++){
		b[m][0] = 0;
	}
	for(n = 0;n < 4;n++){
		b[0][n] = 0;
	}
	LCSLength(String_A,String_B,c,b);
	LCS(6,4,String_A,b);
	print1(String_A,5);
	print1(String_B,3);
	print2(c);
	printf("\n");
	print2(b);
}
void LCSLength(char x[],char y[],int (*c)[4],int (*b)[4]){/*assignment c and b*/
	int i,j;
	for(i = 1;i <= 5;i++){
		c[i][0] = 0;
	}
	for(i = 1;i <= 3;i++){
		c[0][i] = 0;
	}
	for(i = 1;i <= 5;i++){
		for(j = 1;j <= 3;j++){
			if(x[i-1] == y[j-1]){
				c[i][j] = c[i-1][j-1] + 1;
				b[i][j] = 1;
			}else if(c[i-1][j] >= c[i][j-1]){
					c[i][j] = c[i-1][j];
					b[i][j] = 2;
			}else{
					c[i][j] = c[i][j-1];
					b[i][j] = 3;
			}
	
		}
	}
}
void LCS(int i,int j ,char x[],int (*b)[4]){
	char c;
	if(i == 0 || j == 0){
		return;
	}
	if(b[i][j] = 1){
		c = x[i];
		LCS(i-1,j-1,x,b);
		printf("%c\t",c);
	}else if(b[i][j] == 2){
		LCS(i-1,j,x,b);
	}else{
		LCS(i,j-1,x,b);
	}
	printf("\n");
}
void print1(char *m,int n){
	int i ;
	for(i = 0;i < n;i++)
	{
		printf("%c\t",m[i]);
	}
	printf("\n");
}
void print2(int (*n)[4]){
	int i,j;
	for(i = 0;i < 6;i++){
		for(j =0;j < 4;j++){
		printf("%d\t",n[i][j]);
		
		}
		printf("\n");
	}
}
	

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值