/******************************************
* 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");
}
}
小白笔记--------------------构造最长公共子序列
最新推荐文章于 2023-09-20 10:04:01 发布