1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | #include<stdio.h> int lcs(char* a, char* b, int na, int nb); int strlen(char* s); int main(void){ char a[1002]; char b[1002]; int na; int nb; while(scanf("%s %s",a,b)!=EOF){ na=strlen(a); nb=strlen(b); printf("%d\n",lcs(a,b, na, nb)); } return 0; } int strlen(char* s){ char* tmp=s; int count=0; while(*tmp != '\0'){ count++; tmp++; } return count; } int lcs(char* a, char* b, int na, int nb){ int i,j,x,y; int tmp[na+1][nb+1]; for(i=0; i<=na; i++) tmp[i][0]=0; for(i=0; i<=nb; i++) tmp[0][i]=0; for(i=1,x=0; i<=na; i++,x++){ for(j=1,y=0; j<=nb; j++,y++){ if( b[y] == a[x] ) tmp[i][j]=tmp[x][y]+1; else if( tmp[x][j] > tmp[i][y] ) tmp[i][j]=tmp[x][j]; else tmp[i][j]=tmp[i][y]; } } return tmp[na][nb]; } |
Direct link: https://paste.plurk.com/show/148616