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 ...
随机推荐
- 具体解释NoSQL数据库使用实例
一.NoSQL基础知识 1.关于NoSQL 在"NoSQL"一词.实际上是一个叫Racker的同事创造的,当约翰埃文斯埃里克要组织一次活动来讨论开源的分布式数据库. 这个名称和概念 ...
- 9.Nexus私服安装配置
第一步:下载nexus-webapp-1.9.2.4.war包,然后复制到tomcat下的webapps文件夹中 第二步:启动tomcat 第三步:訪问http://localhost:8080/ne ...
- java之JMX
java之JMX 有关JMX的定义和架构就不具体解释了.见百度百科: http://baike.baidu.com/link? url=6QzGGEqphTmpft3ll5mXmDNVRdvLRZhk ...
- 基于二叉树和双向链表实现限制长度的最优Huffman编码
该代码採用二叉树结合双向链表实现了限制长度的最优Huffman编码,本文代码中的权重所有採用整数值表示.http://pan.baidu.com/s/1mgHn8lq 算法原理详见:A fast al ...
- 流媒体技术 rtp/rtcp/rtsp资料精华!
流媒体技术 rtp/rtcp/rtsp资料精华! 流媒体技术 流媒体是指在网络中使用流式(Sreaming)传输技术进行传输的连续时基媒体.如音频数据流或视频数据流,而不是一种新的媒体.流媒体技 ...
- 九度 题目1154:Jungle Roads
题目描写叙述: The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid mon ...
- Linq查询案例
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- Springboot优化
https://www.cnblogs.com/chen110xi/p/6198481.html
- 55.npm install 报错 :stack Error: Can't find Python executable "python"
转自:https://www.cnblogs.com/zengry/p/8044379.html 解决方法 : 1. 安装python , 设置环境变量 :cmd --> path='%path ...
- 5.brackets 快捷键 有大用
转自:https://blog.csdn.net/u012011360/article/details/41209223 ctrl+b 当选中一个文本时,会出现相同的文本,被高亮显示 按ctrl+b ...