Hlg 1618 词频统计.cpp【hash表 + 求字符串hash值】
2013-01-14 23:15
543 查看
[b]题意:[/b]
只有一组数据,数据量为20M
根据单词出现顺序输出出现次数..
[b]思路:[/b]
3种方法:① map ② BKDR求hash值<hash表的线性再散列方法或者是链表形式>
map的方法因为数据量很大..容易超时
[b]Tips:[/b]
※ BKDR求字符串hash值方法:
BKDR+线性再散列
[b]题目链接:[/b]
http://acm.hrbust.edu.cn/index.php?m=ProblemSet&a=showProblem&problem_id=1618
只有一组数据,数据量为20M
根据单词出现顺序输出出现次数..
[b]思路:[/b]
3种方法:① map ② BKDR求hash值<hash表的线性再散列方法或者是链表形式>
map的方法因为数据量很大..容易超时
[b]Tips:[/b]
※ BKDR求字符串hash值方法:
BKDR+线性再散列
#include <stdio.h> #include <string.h> #include <stdlib.h> const int maxn = 180000; int BKDRHash(char *str) { int hash = 0; while (*str) { hash = hash*31 + (*str++); } return (hash & 0x7FFFFFFF); } struct node { int count; char *s; }h[maxn]; int num[maxn]={0}; int tot = 0; void hashinsert(char *str) { int val = BKDRHash(str)%maxn; while (h[val].count != 0) { if (strcmp(h[val].s,str)==0) { h[val].count++; break; } val = (val+1)%maxn; } if (h[val].count == 0) { int len = strlen(str); h[val].s = (char*)malloc(sizeof(char)*(len+1)); strcpy(h[val].s,str); h[val].count++; num[++tot] = val; } } char ss[3000000]; int main() { memset(h,0,sizeof(h)); while (scanf("%s",ss)!=EOF) { hashinsert(ss); } int i; for (i=1; i<=tot; i++) printf("%d\n",h[num[i]].count); return 0; }
[b]题目链接:[/b]
http://acm.hrbust.edu.cn/index.php?m=ProblemSet&a=showProblem&problem_id=1618
相关文章推荐
- vim 字符串出现次数统计(词频)
- hash数组快速查找一个字符串中出现最多的字符,并统计出现的次数
- 字符串操作练习:星座、凯撒密码、99乘法表、词频统计预处理
- java 字符串词频统计实例代码
- C++回顾 统计词频问题 -- vector、map、hash_map(三种方式时间比较)
- 字符串操作练习:星座、凯撒密码、99乘法表、词频统计预处理
- 字符串操作练习:星座、凯撒密码、99乘法表、词频统计预处理
- (2016春) 作业3:个人项目-词频统计
- HDU2017 字符串统计
- 字符串统计(杭电2017)
- 字符串之“统计一个字符串中单词的个数”
- 统计字符串中某个指定字符串的个数
- (2011.11.28) 04_字符串的顺序结构存储.cpp
- 小H的循环节(Hash表-字符串)
- 14-5(2)字符串中大小写字母的统计
- c - 统计字符串"字母,空格,数字,其他字符"的个数和行数.
- 利用word分词来对文本进行词频统计
- java 使用正则表达式统计一个字符串中子字符串出现次数
- jsoncpp封装和解析字符串、数字、布尔值和数组
- 华为机试题---统计并输出每个字符在字符串中出现的次数