The program input is from a text file. Each data set in the file contains two strings representing the given sequences. The sequences are separated by any number of white spaces. The input data are correct. For each set of data the program prints on the standard output the length of the maximum-length common subsequence from the beginning of a separate line.
#include<stdio.h>
#include<string.h>
int c[500][500],lena,lenb;
int max(int a,int b){
if(a>=b)
return a;
else
return b;
}
int main(){
char a[500],b[500];
while(scanf("%s%s",a,b)==2){
lena = strlen(a);
lenb = strlen(b);
for(int i=0;i<lena;i++)
c[i][0]=0;
for(int j=0;j<lenb;j++)
c[0][j]=0;
for(int i=1;i<=lena;i++)
for(int j=1;j<=lenb;j++){
if(a[i-1]==b[j-1])//这里需要注意下,不能忘记字符a[0],b[0]的比较,所以在计算的过程中需要计算到lena
c[i][j]=c[i-1][j-1]+1;
else
c[i][j]=max(c[i-1][j],c[i][j-1]);
}
printf("%d\n",c[lena][lenb]);
}
return 0;
}