最长公共子序列

南阳理工题     最长公共子序列
#include<stdio.h>
#include<string.h>

int a[1001][1001];
int lcs(char *s1, char *s2)
{
    int m=strlen(s1),n=strlen(s2);
    int i,j;
 a[0][0]=0;
 for(i=1;i<=m;++i)a[i][0]=0;
    for(i=1;i<=n;++i)a[0][i]=0;
    for(i=1;i<=m;++i)
  for(j=1;j<=n;++j){
  if(s1[i-1]==s2[j-1])a[i][j]=a[i-1][j-1]+1;
  else if(a[i-1][j]>a[i][j-1])a[i][j]=a[i-1][j];
  else a[i][j]=a[i][j-1];
  }
return a[m][n];
}


int main(){
 int n;
 scanf("%d",&n);
 while(n--){
char c[1008],b[1008];
memset(b,0,sizeof(b));
memset(c,0,sizeof(c));
scanf("%s%s",c,b);
printf("%d\n",lcs(b,c));
 }


return 0;
}

最长公共子序列

上一篇:Codeforces 387B George and Round(贪心)


下一篇:centos 下 boost1.55 编译和安装