您的位置:首页 > 理论基础 > 数据结构算法

一个单词单词字母交换,可得另一个单词,如army->mary,成为兄弟单词。提供一个单词,在字典中找到它的兄弟。描述数据结构和查询过程

2012-10-15 17:52 656 查看
这个解法主要引子一篇博客的解答,我只是实现,方法引于:http://blog.csdn.net/yahohi/article/details/7985328。我实现了第二种方法。


解法一:

使用hash_map和链表。 

首先定义一个key,使得兄弟单词有相同的key,不是兄弟的单词有不同的key。例如,将单词按字母从小到大重新排序后作为其key,比如bad的key为abd,good的key为dgoo。 

使用链表将所有兄弟单词串在一起,hash_map的key为单词的key,value为链表的起始地址。 

开始时,先遍历字典,将每个单词都按照key加入到对应的链表当中。当需要找兄弟单词时,只需求取这个单词的key,然后到hash_map中找到对应的链表即可。 

这样创建hash_map时时间复杂度为O(n),查找兄弟单词时时间复杂度是O(1)。

 

解法二:

同样使用hash_map和链表。

将每一个字母对应一个质数,然后让对应的质数相乘,将得到的值进行hash,这样兄弟单词的值就是一样的了,并且不同单词的质数相乘积肯定不同。

使用链表将所有兄弟单词串在一起,hash_map的key为单词的质数相乘积,value为链表的起始地址。

对于用户输入的单词进行计算,然后查找hash,将链表遍历输出就得到所有兄弟单词。

这样创建hash_map时时间复杂度为O(n),查找兄弟单词时时间复杂度是O(1)。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX_SIZE 287
typedef struct hash_node
{
char *word;
struct hash_node *next;
}hash_node,*hash_map;
hash_map bin[MAX_SIZE]={NULL};
unsigned int get_index(char *pWord)//get hash index
{
int len=strlen(pWord);
int i;
unsigned int index=1;
for(i=0;i<len;i++)
index=index*(pWord[i]-'A'+1);//这里如果是大写字母的话就会使负值,所以要根据情况而定
return index%MAX_SIZE;
}
void insert_word(char *pWord)  //insert word,if collision happens,use link list
{
unsigned int index=get_index(pWord);
printf("%d\n",index);
hash_node *p;
for(p=bin[index];p!=NULL;p=p->next)
if(strcmp(p->word,pWord)==0)
return;
p=(hash_node*)malloc(sizeof(hash_node));
p->word=(char*)malloc(strlen(pWord)+1);
strcpy(p->word,pWord);
p->word[strlen(pWord)]='\0';
p->next=bin[index];// 不断的插入到表头就好,this will be efficient
bin[index]=p;
}
void search_brother(char *pWord) //search brother words
{
unsigned int index=get_index(pWord);
hash_node *p;
for(p=bin[index];p!=NULL;p=p->next)
if(strcmp(pWord,p->word)!=0)
printf("%s\t",p->word);
}
void main()
{
char *string[]={"mary","army","ramy"};
int len=sizeof(string)/sizeof(char*);
int i;
for(i=0;i<len;i++)
insert_word(string[i]);
char word[]="mary";
search_brother(word);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐