最长公共子序列

来源:互联网 发布:nba费舍尔数据 编辑:程序博客网 时间:2024/04/29 18:56
#include<stdio.h>#include<string.h>int s[20][20];int max(int a,int b){ if(a>=b) {  return a; } else {  return b; }}int lcs(int n,int m,char x[],char y[]){ int i,j; for(i=0;i<n;i++) {  s[i][0]=0; } for(i=1;i<m;i++) {  s[0][i]=0; } for(i=0;i<n;i++) {  for(j=0;j<m;j++)  {   if(x[i]==y[j])   {    s[i][j]=s[i-1][j-1]+1;   }   else   {    s[i][j]=max(s[i-1][j],s[i][j-1]);   }  } } return s[n-1][m-1];}int main(){ int s[20][20];    int len1,len2; char x[20],y[20]; while(scanf("%s%s",x,y)!=EOF) {  len1=strlen(x);  len2=strlen(y);  printf("%d\n",lcs(len1,len2,x,y)); } return 0;}