您的位置:首页 > 其它

统计单词次数

2013-03-08 15:52 169 查看
文章:/article/4719916.html

文章中查询每个单词都要遍历 时间复杂度o(n)

改下: 用hash表, o(1)

Word

{

stirng word;

int times;

Word *next;

};

const int MAX=10000000;

Word[MAX] hash;

可以hash表保存每个单词的key

int pos=fun(str);

if(hash[pos]==NULL)

hash[pos]=word;

else

{

Word *p=hash[pos];

while(p!=NULL)

{

if(p->word==str)

{

p->times++;

break;

}

p=p->next;

}

if(p==NULL)

{

Word newword=new Word();

newword->word=str;

newword->times=1;

newword->next=hash[pos];

hash[pos]=newword;

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: