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 ...
随机推荐
- Spark MLlib Deep Learning Convolution Neural Network (深度学习-卷积神经网络)3.3
3.Spark MLlib Deep Learning Convolution Neural Network(深度学习-卷积神经网络)3.3 http://blog.csdn.net/sunbow0 ...
- eclipse部署maven web项目到tomcat服务器时,没有将lib、web.xml复制过去的解决办法
我这几天在写项目的时候发现自己以前的项目能够访问,隔一段时间写的这个项目却不能够访问,没有发现代码的逻辑错,但是就是访问不了jsp页面,项目一发布就是出现404错误,后来发现原来是发布到tomcat上 ...
- java 链接server上的 mongodb 出现 connect time out 问题
异常信息 十二月 22, 2014 5:27:58 下午 com.mongodb.DBTCPConnector initDirectConnection 警告: Exception executing ...
- 归并排序_分治算法 (白书P226)
#include<iostream> #include<cstdio> #include<algorithm> using namespace std; int a ...
- 10.axis实现webservices分布式通信
转自:https://www.aliyun.com/jiaocheng/310112.html 分布式通信原理 基本原理:stub和skeleton作为客户端和服务端传输的中介,stub和skelet ...
- css中margin上下外边距重叠问题
css的盒子模型里是这样规定两个对象之间的距离的:对象之间的间距是由两个对象的盒子模型的最终计算值得出来的,也就是说两个对象之间的间距就是两个对象的距离,但是当遇到两个对象一个有下外边距margin, ...
- HDU 2689 Tree
Tree Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- 通过Gradle来下载依赖的jar包
前段时间在看Gradle文章方向其比ANT.Maven真的要简单很多,特别是在下载jar依赖方面下面就是用gradle写的一个jar包依赖下载的小例子: (1)建立build.gradle文件 (2) ...
- BEGINNING SHAREPOINT® 2013 DEVELOPMENT 第9章节--client对象模型和REST APIs概览 托管代码(.NET)
BEGINNING SHAREPOINT® 2013 DEVELOPMENT 第9章节--client对象模型和REST APIs概览 托管代码(.NET) 在SP2010中,微软提 ...
- Windows 98 二十岁了,这些功能都是从它开始的(虽然 Windows 98 不如 Windows 95 那样具有革命性,但完成度更高,更加成熟。到最后还是:相见不如怀念。)
1998 年 6 月 25 日午夜,美国著名连锁零售店 CompUSA 门外挤满了狂热的消费者和媒体,他们在等待一款软件发售:Windows 98,即使明知它要到当天早上才正式上市. ▲ 在 Comp ...