Created
June 27, 2016 10:26
-
-
Save taznica/14104ab885297276d574614259e830ab to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 dynamic(char *X, int m, char *Y, int n) | |
{ | |
int A[101][101]; | |
int i, j; | |
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; | |
} | |
for(i=1;i<m+1;i++) | |
{ | |
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); | |
} | |
} | |
} | |
return A[m][n]; | |
} | |
int main() | |
{ | |
int v, i, m, n, min=100; | |
char X[25]; | |
char word[25]; | |
scanf("%d", &v); | |
char dict[v][25]; | |
for(i=0;i<v;i++) | |
{ | |
scanf("%s\n", dict[i]); | |
} | |
scanf("%s", X); | |
printf("%s\n", X); | |
for(i=0;i<v;i++) | |
{ | |
m=strlen(X); | |
n=strlen(dict[i]); | |
printf("%d %d\n", m, n); | |
if(min>dynamic(X, m, dict[i], n)) | |
{ | |
min=dynamic(X, m, dict[i], n); | |
printf("%d\n",min); | |
strcpy(word, dict[i]); | |
} | |
} | |
printf("%s\n", word); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment