UVA 10391 - Compound Words 字符串hash
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1332
题目大意:
给定一个词典(已经按照字典序排好),要求找出其中所有的复合词,即恰好由两个单词连接而成的单词。(按字典序输出)
思路:
对于每个单词,存入Hash表,然后对每个单词拆分。
Hash函数的选取可以看:https://www.byvoid.com/blog/string-hash-compare/
我的这个是BKDRHash。
乘以一个比他大的素数。
关于:0x7fffffff(即int_max,最大的整型范围。why? 16进制每位由4个二进制表示,7二进制位0111,其他的f为1111.)
#include<cstdio>
#include<cstring>
const int MAXN=120000+10;
int head[MAXN],len,n;
char data[MAXN][30];
typedef unsigned long long LL;
struct edge
{
int index,next;
}e[MAXN]; int gethash(char *s)
{
LL seed=131;
LL res=0;
int L=strlen(s);
for(int i=0;i<L;i++)
{
res=res*seed+s[i];
}
return (res& 0x7fffffff )% MAXN;
} void add(char *s,int index)
{
int id=gethash(s);
e[len].index=index;
e[len].next=head[id];
head[id]=len++;
}
bool find(char *s)
{
int id=gethash(s);
for(int i=head[id];i!=-1;i=e[i].next)
{
int index=e[i].index;
if(strcmp(s,data[i])==0)
return true;
}
return false;
}
int main()
{
n=len=0;
memset(head,-1,sizeof(head));
while(~scanf("%s",data[n]))
{
add(data[n],n);
n++;
} for(int i=0;i<n;i++)
{
int L=strlen(data[i]);
L--;
for(int j=1;j<L;j++)
{
char temp[30];
strcpy(temp,data[i]+j);
char flag=data[i][j];
data[i][j]='\0';
if(find(data[i]) && find(temp))
{
data[i][j]=flag;
puts(data[i]);
break;
}
data[i][j]=flag;
}
} return 0;
}
UVA 10391 - Compound Words 字符串hash的更多相关文章
- uva 10391 Compound Words <set>
Compound Words You are to find all the two-word compound words in a dictionary. A two-word compound ...
- UVA 10391 Compound Words
Problem E: Compound Words You are to find all the two-word compound words in a dictionary. A two-wor ...
- [知识点]字符串Hash
1.前言 字符串的几大主要算法都多少提及过,现在来讲讲一个称不上什么算法, 但是非常常用的东西——字符串Hash. 2.Hash的概念 Hash更详细的概念不多说了,它的作用在于能够对复杂的状态进行简 ...
- 【BZOJ-3555】企鹅QQ 字符串Hash
3555: [Ctsc2014]企鹅QQ Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 1545 Solved: 593[Submit][Statu ...
- POJ 1200 字符串HASH
题目链接:http://poj.org/problem?id=1200 题意:给定一个字符串,字符串只有NC个不同的字符,问这个字符串所有长度为N的子串有多少个不相同. 思路:字符串HASH,因为只有 ...
- LA4671 K-neighbor substrings(FFT + 字符串Hash)
题目 Source http://acm.hust.edu.cn/vjudge/problem/19225 Description The Hamming distance between two s ...
- 各种字符串Hash函数比较(转)
常用的字符串Hash函数还有ELFHash,APHash等等,都是十分简单有效的方法.这些函数使用位运算使得每一个字符都对最后的函数值产生影响.另外还有以MD5和SHA1为代表的杂凑函数,这些函数几乎 ...
- 字符串hash + 二分答案 - 求最长公共子串 --- poj 2774
Long Long Message Problem's Link:http://poj.org/problem?id=2774 Mean: 求两个字符串的最长公共子串的长度. analyse: 前面在 ...
- 字符串hash - POJ 3461 Oulipo
Oulipo Problem's Link ---------------------------------------------------------------------------- M ...
随机推荐
- hzwer 模拟题 祖孙询问
祖孙询问 题目描述 已知一棵n个节点的有根树.有m个询问.每个询问给出了一对节点的编号x和y,询问x与y的祖孙关系. 输入输出格式 输入格式: 输入第一行包括一个整数n表示节点个数. 接下来n行每行一 ...
- Objective-C(十九、通知-消息发送模式之中的一个)——iOS开发基础
结合之前的学习笔记以及參考<Objective-C编程全解(第三版)>,对Objective-C知识点进行梳理总结. 知识点一直在变.仅仅是作为參考.以苹果官方文档为准~ 十九.通知-消息 ...
- [Parcel] Bundle a React App with Parcel
Parcel comes in as the new cool kid in the bundlers world. Unlike other bundlers which take lots of ...
- CSS动态实现文本框清除按钮的隐藏与显示
当前现代浏览器中,Chrome浏览器下type=search的输入框会有清除按钮的动态呈现,不过搜索input框尺寸不太好控制(padding无视):FireFox浏览器貌似任何类型的输入框都无动于衷 ...
- SortedDictionary<TKey, TValue> 类 表示根据键进行排序的键/值对的集合。
SortedDictionary<TKey, TValue> 类 表示根据键进行排序的键/值对的集合. SortedDictionary<TKey, TValue> 中的每 ...
- MySql_Learn
1 id 自增长 auto_increment 2 获取当前时间 now() 3 新增字段 修改字段名称 简单分页功能 limit 10 offset 20; 查询第21到30条数据 selec ...
- BZOJ3336: Uva10572 Black and White(插头Dp)
解题思路: 分类讨论即可. 代码(懒得删Debug了): #include<map> #include<cstdio> #include<vector> #incl ...
- Scala具体解释---------类
Scala中的类 摘要: 在本篇中.你将会学习怎样用Scala实现类. 假设你了解Java或C++中的类,你不会认为这有多难.而且你会非常享受Scala更加精简的表示法带来的便利.本篇的要点包含: 1 ...
- Linux下读写芯片的I2C寄存器
要想在Linux下读写芯片的I2C寄存器,一般需要在Linux编写一份该芯片的I2C驱动,关于Linux下如何编写I2C驱动,前一篇文章<手把手教你写Linux I2C设备驱动>已经做了初 ...
- 11.使用 package.json
转自:http://www.runoob.com/nodejs/nodejs-express-framework.html package.json 位于模块的目录下,用于定义包的属性.接下来让我们来 ...