统计单词次数
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;
}
}
文章中查询每个单词都要遍历 时间复杂度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;
}
}
相关文章推荐
- 利用键值对统计单词出现的次数
- C# 实现统计字符串中不同单词的出现次数【百度笔试题】
- c程序设计语言_习题1-13_统计输入中单词的长度,并且根据不同长度出现的次数绘制相应的直方图
- java统计一篇文章中单词出现的次数
- Java统计一篇文章中出现次数最多的汉字或英文单词 又出现次数的统计
- Java 统计一个字符串中每个单词,或者字母出现的次数
- shell统计文本中单词的出现次数
- Hadoop 统计文件中某个单词出现的次数
- 统计一篇英文文章内每个单词出现频率,并返回出现频率最高的前10个单词及其出现次数
- 统计并输出所读入的单词出现的次数
- java统计一篇文章中单词出现的次数
- python---在文本/文章中统计字符串中出现单词次数
- Java统计一篇英文单词出现次数
- 用python统计文件中各个单词出现的次数
- JS使用单链表统计英语单词出现次数
- 练习2-4:编一个程序统计文件中特定单词出现的次数(要求使用string类的运算符==来查找单词)
- Python统计单词出现的次数
- 统计海量文章内容中出现次数前K大的单词并输出(完整实现)
- 统计分析文章中英文单词出现次数及频率(C++实现)
- 练习2-4:编一个程序统计文件中特定单词出现的次数(要求使用string类的运算符==来查找单词) .