求两个字符串的最长公共子序列的长度(动态规划)

# include<stdio.h>
# include<string.h>
# define N 1000
int str[N+2][N+2];
int max(int a,int b)
{
 if(a>=b) return a;
 else return b;
}
int main()
{
 int i,j,la,lb;
 char str1[N+1],str2[N+1];
 while(scanf("%s%s",str1,str2)!=EOF)
 {
  la=strlen(str1);
  lb=strlen(str2);
  
  for(i=0;i<=la;i++)
   str[0][i]=0;
  for(j=0;j<=lb;j++)
   str[j][0]=0;
  
  for(j=0;j<la;j++)
   for(i=0;i<lb;i++)
   {
    if(str1[j]==str2[i])
     str[i+1][j+1]=str[i][j]+1;
    else str[i+1][j+1]=max(str[i][j+1],str[i+1][j]);
   }
   
   printf("%d/n",str[lb][la]);
    }
 return 0;
}

posted on 2012-01-26 17:35  [S*I]SImMon_WCG______*  阅读(899)  评论(0编辑  收藏  举报

导航