char * dict[]={
"bland",
"blank",
"bleak",
"bleed",
"blend",
"blind",
"blink",
"blond",
"blood",
"bloom",
"blown",
"blows",
"brand",
"brank",
"bread",
"break",
"bream",
"breed",
"brown",
"clank",
"clink",
"dread",
"dream"
};
#define MAX_DICT 23
checkin(char * input){
int i;
for(i=0;i<MAX_DICT;i++)
{
if(strcmp(input,dict[i])==0)return i;
}
return -1;
}
getNext(char * input,int count,int indexes[])
{
int i,j,len,dest,k,cc;
char new[100];
len=strlen(input);
for(i=0;i<len;i++)
{
strcpy(new,input);
for(j=0;j<26;j++)
{
new[i]='a'+j;
cc=0;
for(k=0;k<count;k++){
if(strcmp(dict[indexes[k]],new)==0){
cc=1;
break;
}
}
if(cc==1)continue;//已經變換過
dest=checkin(new);
if(dest>=0)return dest;//找到可以變換的詞
}
}
return -1;//沒有可以變換的詞
}
main(){
char input[100],new[100],output[100],road[1024];
int index,i,j,dest,count,indexes[MAX_DICT];
strcpy(input,"");
while(strcpy(input,"quit")!=0)
{
printf("請輸入變換字符串:");
scanf("%s",input);
if(strcmp(input,"quit")==0)break;
printf("請輸入目標字符串:");
scanf("%s",output);
if(strcmp(output,"quit")==0)break;
index=checkin(output);
if(index<0){
printf("%s 不在字典裏!\n",output);
continue;
}
index=checkin(input);
if(index<0){
printf("%s 不在字典裏!\n",input);
continue;
}
strcpy(road,"");
strcpy(new,input);
count=0;
indexes[count]=index;
count++;
while(1)
{
dest=-1;
dest=getNext(new,count,indexes);
if(dest<0){
printf("不存在變換系列\n");
break;
}
else if(strcmp(dict[dest],output)==0){//找到目標字符串
printf("%s,%s%s\n",input,road,output);
break;
}
else{//變換過程,如果成功變換則輸出
strcat(road,dict[dest]);
strcat(road,",");
strcpy(new,dict[dest]);
indexes[count]=dest;
count++;
}
}
}
}