I've tried to solve this problem by:
1.computing the distance of the words frm the 1st word.
2. Sort(ascending) the words on the basis of the distances and output them all.
But, i'm getting OLE. I don't know why. Can someone help me with some suggetions. And is anything wrong in my approach.
Code: Select all
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct Word{
char word[1005];//edited after AC
int d;
} Word;
Word word[1005];//edited after AC
int comp(const void* a, const void *b){
Word* x=(Word*) a;
Word* y=(Word*) b;
return x->d - y->d;
}
void main(){
int i,j,m,n;
// freopen("C:\\4.txt","r",stdin);
while(scanf("%d %d",&m,&n)==2){
getchar();
for(i=0;i<m;i++){
word[i].d=0;
gets(word[i].word);
for(j=0;j<n;j++) if(word[0].word[j]!=word[i].word[j]) word[i].d++;
}
qsort(word,m,sizeof(Word),comp);
for(i=0;i<m;i++) puts(word[i].word);
}
}