字符串哈希hash
题目描述
如题,给定N个字符串(第i个字符串长度为Mi,字符串内包含数字、大小写字母,大小写敏感),请求出N个字符串中共有多少个不同的字符串。
友情提醒:如果真的想好好练习哈希的话,请自觉,否则请右转PJ试炼场:)
输入输出格式
输入格式:
第一行包含一个整数N,为字符串的个数。
接下来N行每行包含一个字符串,为所提供的字符串。
输出格式:
输出包含一行,包含一个整数,为不同的字符串个数。
输入输出样例
5
abc
aaaa
abc
abcc
12345
4
说明
时空限制:1000ms,128M
数据规模:
对于30%的数据:N<=10,Mi≈6,Mmax<=15;
对于70%的数据:N<=1000,Mi≈100,Mmax<=150
对于100%的数据:N<=10000,Mi≈1000,Mmax<=1500
样例说明:
样例中第一个字符串(abc)和第三个字符串(abc)是一样的,所以所提供字符串的集合为{aaaa,abc,abcc,12345},故共计4个不同的字符串。
Tip: 感兴趣的话,你们可以先看一看以下三题:
BZOJ3097:http://www.lydsy.com/JudgeOnline/problem.php?id=3097
BZOJ3098:http://www.lydsy.com/JudgeOnline/problem.php?id=3098
BZOJ3099:http://www.lydsy.com/JudgeOnline/problem.php?id=3099
如果你仔细研究过了(或者至少仔细看过AC人数的话),我想你一定会明白字符串哈希的正确姿势的^_^
字符串hash,把每一位当做一个某进制大数的一位,乘积后mod一个素数
在此讲解三种姿势
1.ull自然溢出
100分
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = ;
typedef unsigned long long ull;
int hash[maxn];
int base=;
char s[maxn]; int n,ans=;
ull h(char *s) {
int len =strlen(s);
ull ans=;
for(int i=;i<len;++i)
ans=ans*(ull)base+(ull)s[i];
return ans&0x7fffffff;
}
int main () {
scanf("%d",&n);
for(int i=;i<=n;++i) {
scanf("%s",s);
hash[i]=h(s);
}
sort(hash+,hash+n+);
for(int i=;i<=n;++i)
if(hash[i]!=hash[i-])
ans++;
printf("%d\n",ans);
return ;
}
2.对单素数取mod
70分
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = ;
typedef unsigned long long ull;
ull mod = ;
int hash[maxn];
int base=;
char s[maxn]; int n,ans=;
ull h(char *s) {
int len =strlen(s);
ull ans=;
for(int i=;i<len;++i)
ans=(ans*(ull)base+(ull)s[i])%mod;
return ans;
}
int main () {
scanf("%d",&n);
for(int i=;i<=n;++i) {
scanf("%s",s);
hash[i]=h(s);
}
sort(hash+,hash+n+);
for(int i=;i<=n;++i)
if(hash[i]!=hash[i-])
ans++;
printf("%d\n",ans);
return ;
}
对双素数取mod
100分
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = ;
typedef unsigned long long ull;
const int mod1 = ;
const int mod2 = ;
struct data{
int x, y;
bool operator < (const data &a)const {
return x< a.x;
}
}hash[maxn];
int base=;
char s[maxn]; int n,ans=;
int h1(char *s) {
int len =strlen(s);
int ans=;
for(int i=;i<len;++i)
ans=(ans*base+s[i])%mod1;
return ans;
}
int h2(char *s) {
int len =strlen (s);
int ans=;
for(int i=;i<len;++i)
ans=(ans*base+s[i])%mod2;
return ans;
}
int main () {
scanf("%d",&n);
for(int i=;i<=n;++i) {
scanf("%s",s);
hash[i].x=h1(s);
hash[i].y=h2(s);
}
sort(hash+,hash+n+);
for(int i=;i<=n;++i)
if(hash[i].x!=hash[i-].x||hash[i].y!=hash[i-].y)
ans++;
printf("%d\n",ans);
return ;
}
字符串哈希hash的更多相关文章
- Crazy Search POJ - 1200 (字符串哈希hash)
Many people like to solve hard puzzles some of which may lead them to madness. One such puzzle could ...
- 牛客练习赛33 E tokitsukaze and Similar String (字符串哈希hash)
链接:https://ac.nowcoder.com/acm/contest/308/E 来源:牛客网 tokitsukaze and Similar String 时间限制:C/C++ 2秒,其他语 ...
- luoguP3370 【模板】字符串哈希 [hash]
题目描述 如题,给定N个字符串(第i个字符串长度为Mi,字符串内包含数字.大小写字母,大小写敏感),请求出N个字符串中共有多少个不同的字符串. 友情提醒:如果真的想好好练习哈希的话,请自觉,否则请右转 ...
- 从Hash Killer I、II、III论字符串哈希
首先,Hash Killer I.II.III是BZOJ上面三道很经典的字符串哈希破解题.当时关于II,本人还琢磨了好久,但一直不明白为啥别人AC的代码都才0.3kb左右,直到CYG神犇说可以直接随机 ...
- HASH 字符串哈希 映射转化
哈希HASH的本质思想类似于映射.离散化. 哈希,通过给不同字符赋不同的值.并且钦定一个进制K和模数,从而实现一个字符串到一个模意义下的K进制数上. 它的主要目的是判重,用于$DFS$.$BFS$判重 ...
- Redis支持的数据类型及相应操作命令:String(字符串),Hash(哈希),List(列表),Set(集合)及zset(sorted set:有序集合)
help 命令,3种形式: help 命令 形式 help @<group> 比如:help @generic.help @string.help @hash.help @list.hel ...
- Redis 命令,键(key),字符串(String),哈希(Hash),列表(List),集合(Set)(二)
Redis 命令 Redis 命令用于在 redis 服务上执行操作. 要在 redis 服务上执行命令需要一个 redis 客户端.Redis 客户端在我们之前下载的的 redis 的安装包中. ...
- 【基本算法入门-字符串哈希(Hash)】-C++
字符串哈希入门 说得通俗一点,字符串哈希实质上就是把每个不同的字符串转成不同的整数. 为什么会有这样的需要呢?很明显,存储一个超长的字符串和存储一个超大但是能存的下的整数,后者所占的空间会少的多,但主 ...
- HDU 1880 魔咒词典(字符串哈希)
题目链接 Problem Description 哈利波特在魔法学校的必修课之一就是学习魔咒.据说魔法世界有100000种不同的魔咒,哈利很难全部记住,但是为了对抗强敌,他必须在危急时刻能够调用任何一 ...
随机推荐
- Bootstrap历练实例:超小的按钮
<!DOCTYPE html><html><head> <meta http-equiv="Content-Type" content=& ...
- 冒泡法排序参考(Java)
package com.swift; public class Maopao { //冒泡法 public static void main(String[] args) { int[] arr= { ...
- javascript(九)事件冒泡 onmouseenter onmouseenter 默认事件 和 键盘事件
1 事件冒泡 子元素触发的事件,会往上(父元素)传递: 例子: <div id="box"> <p></p> </div> < ...
- 如何用纯 CSS 创作一个蝴蝶标本展示框
效果预览 在线演示 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/xzgZzQ 可交互视频教 ...
- Kali入门配置使用(一)
一.Kali简介 1.1.相关连接 Kali百度百科:https://baike.baidu.com/item/Kali%20linux/8305689?fr=aladdin Kali wiki:ht ...
- 【java】 field 和 variable 区别及相关术语解释
Having said that, the remainder of this tutorial uses the following general guidelines when discussi ...
- leepcode作业解析 - 5-19
18.两数之和II -输入有序数组 给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数. 函数应该返回这两个下标值 index1 和 index2,其中 index1 必须小于 ...
- Day07 数据类型(列表,元组,字典,集合)常用操作和内置方法
数据类型 列表list: 用途:记录多个值(同种属性) 定义方式:[]用逗号分隔开多个任意类型的值 list()造出来的是列表,参数是可迭代对像,也就是可以使用for循环的对像 传入字典,出来的列表元 ...
- DSP中-stack和-heap的作用
-stack 0x00000800-heap 0x00000800 stack - 又称系统栈(system stack),用于: 保存函数调用后的返回地址; ...
- 1. Go的安装和第一行代码
Go 语言环境安装 Go 语言支持以下系统: Linux FreeBSD Mac OS X(也称为 Darwin) Windows 安装包下载地址为:https://golang.org/dl/. 如 ...