C语言程序算法
发布网友
发布时间:2022-06-13 02:22
我来回答
共2个回答
热心网友
时间:2023-10-11 07:20
#include <stdio.h>
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++;
}
}
}
}
热心网友
时间:2023-10-11 07:21
一共就5个字符,5层遍历循环就可以实现所有的分类,然后判断就行了。不过效率嘛。。。肯定是最差的。
如果要考虑效率最优的话,那么你给的分太少了