1 最长公共子序列(just work 递归实现)
1 #include<stdlib.h> 2 #include<stdio.h> 3 int lcs(char a[], char b[],int na,int nb,int j); 4 int main(void){ 5 char a[]="abcdefg"; 6 char b[]="bdcedeabf"; 7 int na=7; 8 int nb=9; 9 int j=0; 10 int l=lcs(a,b,na,nb,j); 11 printf("%d",l); 12 return 0; 13 } 14 15 int lcs(char a[], char b[],int na,int nb,int j){ 16 if(na==0 || nb==0){ 17 return j; 18 } 19 if(a[na-1]==b[nb-1]){ 20 return lcs(a,b,na-1,nb-1,j+1); 21 }else{ 22 int left = lcs(a,b,na-1,nb,j); 23 int right = lcs(a,b,na,nb-1,j); 24 return left>right?left:right; 25 } 26 }