字符串哈希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种不同的魔咒,哈利很难全部记住,但是为了对抗强敌,他必须在危急时刻能够调用任何一 ...
随机推荐
- iOS Crash
常见原因及解决方法: 1. 访问数组类对象越界或插入了空对象NSMutableArray/NSMutableDictionary/NSMutableSet 等类下标越界,或者 insert 了一个 n ...
- Xcode 6 创建 Objective-C category
1. Command + N 2. 选择 iOS - Source - Objective-C File 3.File Type 选择 Category,Class 填基于的类名,File填扩展的名
- 用函数式编程思维解析anagrams函数
//函数式编程思维分析 这个排列函数 const anagrams = str => { if (str.length <= 2) return str.length === 2 ? [s ...
- [LUOGU] P1048 采药
题目描述 辰辰是个天资聪颖的孩子,他的梦想是成为世界上最伟大的医师.为此,他想拜附近最有威望的医师为师.医师为了判断他的资质,给他出了一个难题.医师把他带到一个到处都是草药的山洞里对他说:" ...
- jsDate()
var myTime=new Date();//myTime的数据类型为(typeof) object //下面得到的都为number 类型 getFullYear();年 四位数字返回年份. get ...
- python 中变量和对象
1. 在 python 中,类型属于对象,变量是没有类型的:a=[1,2,3] a="Runoob"以上代码中,[1,2,3] 是 List 类型,"Runoob&quo ...
- Verilog学习笔记基本语法篇(三)·········赋值语句(待补充)
在Verilog HDL语言中,信号有两种赋值方式. A)非阻塞赋值(Non-Blocking)方式(如:b<=a;) (1)在语句块中,上面语句所赋值的变量不能立即为下面的语句所用: (2)块 ...
- Android自动化测试Uiautomator--UiCollection接口简介
这个对象可以理解为一个对象的集合,因为UiSelector描述后得到的有可能是多个满足条件的控件集合,因此可以用来生成UiCollection,继承自UiObject. 用于枚举一个容器的用户界面(U ...
- Idea使用Tomcat乱码 tomcat 9.0 8.5.37乱码
使用新版tomcat 如8.5.37,9.0.14的时候idea控制台输出乱码,很简单老版本的如8.5.31就不会乱码,使用比较工具比较一下发现如下变化, 关键的关键是\apache-tomcat-8 ...
- python基础学习笔记——迭代器
我们之前一直在用可迭代对象进行操作,那么到底什么是可迭代对象.我们现在就来讨论讨论可迭代对象.首先我们先回顾下我们 熟知的可迭代对象有哪些: str list tuple dic set ...