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的更多相关文章

  1. uva 10391 Compound Words <set>

    Compound Words You are to find all the two-word compound words in a dictionary. A two-word compound ...

  2. UVA 10391 Compound Words

    Problem E: Compound Words You are to find all the two-word compound words in a dictionary. A two-wor ...

  3. [知识点]字符串Hash

    1.前言 字符串的几大主要算法都多少提及过,现在来讲讲一个称不上什么算法, 但是非常常用的东西——字符串Hash. 2.Hash的概念 Hash更详细的概念不多说了,它的作用在于能够对复杂的状态进行简 ...

  4. 【BZOJ-3555】企鹅QQ 字符串Hash

    3555: [Ctsc2014]企鹅QQ Time Limit: 20 Sec  Memory Limit: 256 MBSubmit: 1545  Solved: 593[Submit][Statu ...

  5. POJ 1200 字符串HASH

    题目链接:http://poj.org/problem?id=1200 题意:给定一个字符串,字符串只有NC个不同的字符,问这个字符串所有长度为N的子串有多少个不相同. 思路:字符串HASH,因为只有 ...

  6. LA4671 K-neighbor substrings(FFT + 字符串Hash)

    题目 Source http://acm.hust.edu.cn/vjudge/problem/19225 Description The Hamming distance between two s ...

  7. 各种字符串Hash函数比较(转)

    常用的字符串Hash函数还有ELFHash,APHash等等,都是十分简单有效的方法.这些函数使用位运算使得每一个字符都对最后的函数值产生影响.另外还有以MD5和SHA1为代表的杂凑函数,这些函数几乎 ...

  8. 字符串hash + 二分答案 - 求最长公共子串 --- poj 2774

    Long Long Message Problem's Link:http://poj.org/problem?id=2774 Mean: 求两个字符串的最长公共子串的长度. analyse: 前面在 ...

  9. 字符串hash - POJ 3461 Oulipo

    Oulipo Problem's Link ---------------------------------------------------------------------------- M ...

随机推荐

  1. 【Uva 1601】The Morning after Halloween

    [Link]: [Description] 给你一张平面图; 最多可能有3只鬼; 给出这几只鬼的初始位置; 然后,这几只鬼有各自的终点; 每秒钟,这几只鬼能同时移动到相邻的4个格子中的一个 任意两只鬼 ...

  2. linux杂谈(十八):DNSserver的配置(一)

    1.DNSserver简单介绍 域名系统(英文:Domain Name System,縮寫:DNS)是因特网的一项服务. 它作为将域名和IP地址相互映射的一个分布式数据库,可以使人更方便的訪问互联网. ...

  3. Codeforces 472D

    看官方题解提供的是最小生成树,怎么也想不明确.you can guess and prove it! 看了好几个人的代码.感觉实现思路全都不一样,不得不佩服cf题目想法的多样性 以下说说我自己的理解, ...

  4. 3.cocos代码入口

    模拟代码进入过程: main.cpp #include <iostream> #include "AppDelegate.h" #include "CCApp ...

  5. 深入了解"网上邻居"原理

    说到“网上邻居”,相信很多人都很熟悉.但是说起“网上邻居”的工作机制,可能大家就不太清楚了. 要说“网上邻居”的工作机制,不妨联系一下生活中的例子:比如我(A),要拜访一个远方的朋友(B),我要去他的 ...

  6. PHP生成二维码方法

    <?php //先下载一份phpqrcode类,下载地址http://down.51cto.com/data/780947require_once("phpqrcode/phpqrco ...

  7. 洛谷——P2957 [USACO09OCT]谷仓里的回声Barn Echoes

    https://www.luogu.org/problem/show?pid=2957 题目描述 The cows enjoy mooing at the barn because their moo ...

  8. Chormium线程模型及应用指南

    核心概念 设计上遵循以下原则: 1 不要在UI线程做不论什么堵塞式的I/O操作,以及其他耗时的操作,通过消息传递把各种操作传给相应用途的线程去做. 2 不鼓舞线程加锁机制和线程安全对象. 对象仅仅存在 ...

  9. 【基础练习】【线性DP】codevs2622 数字序列(最大连续子序列和)题解

    版权信息 转载请注明出处 [ametake版权全部]http://blog.csdn.net/ametake欢迎来看 这道题目本质就是朴素的最大连续子序列和 直接上题目和代码 题目描写叙述 Descr ...

  10. linux host主机名配置

    1.查看主机名 #hostname 2.查看ip #ifconfig 2.添加主机名配置 #vi /etc/hosts 新增一行 172.23.26.195 vhost145.idmp.safe