Skip to content

Instantly share code, notes, and snippets.

@taznica
Created June 27, 2016 03:25
Show Gist options
  • Save taznica/954dbe4ba336fa448aba66e04f7be73e to your computer and use it in GitHub Desktop.
Save taznica/954dbe4ba336fa448aba66e04f7be73e to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <string.h>
int min3(int a, int b, int c)
{
int temp;
if(b>c)
{
temp=b;
b=c;
c=temp;
}
if(a>b)
{
temp=a;
a=b;
b=temp;
}
return a;
}
int main()
{
char X[100], Y[100];
int m, n, i, j;
int A[101][101];
scanf("%s", X);
scanf("%s", Y);
m=strlen(X);
n=strlen(Y);
/* Aの初期化 */
A[0][0]=0;
for(j=0;j<n+1;j++)
{
A[0][j]=j;
}
for(i=0;i<m+1;i++)
{
A[i][0]=i;
}
/* 1行目(Y) */
printf(" ");
for(j=0;j<n-1;j++)
{
printf("%c ", Y[j]);
}
printf("%c\n", Y[n-1]);
/* 2行目 */
printf(" ");
for(j=0;j<n;j++)
{
printf("%d ", A[0][j]);
}
printf("%d\n", A[0][n]);
/* 3行目以降 */
for(i=1;i<m+1;i++)
{
printf("%c ", X[i-1]);
printf("%d ", A[i][0]);
for(j=1;j<n+1;j++)
{
if(X[i-1]==Y[j-1])
{
A[i][j]=min3(A[i-1][j-1], A[i-1][j]+1, A[i][j-1]+1);
}
else
{
A[i][j]=min3(A[i-1][j-1]+1, A[i-1][j]+1, A[i][j-1]+1);
}
}
for(j=1;j<n;j++)
{
printf("%d ", A[i][j]);
}
printf("%d\n", A[i][n]);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment