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 ...
随机推荐
- 【Uva 10723】Cyborg Genes
[Link]: [Description] 给你两个串s1,s2; 让你生成一个串S; 使得s1和s2都是S的子列; 要求S最短; 求S的不同方案个数; [Solution] 设两个串的长度分别为n1 ...
- CSUOJ 1542 Flipping Parentheses
ACM International Collegiate Programming Contest Asia Regional Contest, Tokyo, 2014–10–19 Problem G ...
- RQNOJ PID496/[IOI1999]花店橱窗布置
PID496 / [IOI1999]花店橱窗布置 ☆ 题目描述 某花店现有F束花,每一束花的品种都不一样,同时至少有同样数量的花瓶,被按顺序摆成一行,花瓶的位置是固定的,从左到右按1到V顺序 编号 ...
- 如何获取repeater某行第一列的值
<div> <asp:Repeater ID="Repeater1" runat="server" DataMember="Defa ...
- ADB高级应用
ADB高级应用 一.利用无线来查看adb shell > adb tcpip 5555 连接: > adb connect IP:5555 见后文<调试注意事项> 二.模拟按键 ...
- 參考mudo logging写的win下logging
#pragma once #include <boost/noncopyable.hpp> #include <boost/scoped_ptr.hpp> #include & ...
- wifidog用php实现验证流程
原创性声明 此博文的出处 为 http://blog.csdn.net/zhujunxxxxx/article/details/25384909假设进行转载请注明出处.本文作者原创,邮箱zhujunx ...
- RISC-V指令集的诞生,"V"也表示变化(variation)和向量(vectors)
RISC-V登场,Intel和ARM会怕吗? 张竞扬 摩尔精英 摩尔精英.创始人兼CEO 82 人赞了该文章 在2015年12月的Nature网站上,由U.C. Berkeley等几个大学的研究人员主 ...
- u盘安装14.04ubuntu系统
最近在安装ubuntu 14.04系统,总结了下安装的方法,记录如下 1.下载ubuntu 14.04 iso文件,下载地址 http://www.ubuntu.com/download/deskto ...
- View State
如何查看viewstate 鼠标右键页面,然后view page source 源码中搜索viewstate,会找到一个隐藏的字段. <input type="hidden" ...