Skip to content

Instantly share code, notes, and snippets.

@Yi-Tseng
Created September 22, 2012 16:46
Show Gist options
  • Save Yi-Tseng/3766713 to your computer and use it in GitHub Desktop.
Save Yi-Tseng/3766713 to your computer and use it in GitHub Desktop.
#include<stdio.h>
#define M 5
#define N 6
int max(a,b){
return a>=b?a:b;
}
int main(){
int x[M] = { 1, 1, 2, 3, 2 };
int y[N] = { 2, 2, 4, 3, 1, 2 };
int c[M][N];
int i,j;
for(i=0;i<M;i++){
for(j=0;j<N;j++){
if(i == 0 || j ==0){
c[i][j] = 0;
}else if(x[i] == y[j]){
c[i][j] = c[i-1][j-1] + 1;
}else if(x[i] != y[j]){
c[i][j] = max(c[i-1][j],c[i][j-1]);
}
}
}
for(i=0;i<M;i++){
for(j=0;j<N;j++){
printf("%d\t",c[i][j]);
}
printf("\n");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment