字符串哈希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种不同的魔咒,哈利很难全部记住,但是为了对抗强敌,他必须在危急时刻能够调用任何一 ...
 
随机推荐
- Python-DB接口规范
			
threadsafety 线程安全级别.threadsafety 这是一个整数, 取值范围如下: 0:不支持线程安全, 多个线程不能共享此模块 1:初级线程安全支持: 线程可以共享模块, 但不能共享连 ...
 - SQLyog连接数据库 提示错误plugin caching_sha2_password could not be loaded
			
1.打开mysql cmd 2.执行语句 ALTER USER 'root'@'localhost' IDENTIFIED BY 'password' PASSWORD EXPIRE NEVER; # ...
 - 121. Best Time to Buy and Sell Stock@python
			
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
 - PAT 乙级 1033
			
题目 题目地址:PAT 乙级 1033 题解 本题一直有一分的样例点过不了,原因在于用了cin接收输入的字符…… 问题在于:如果用cin输入,无法接收无坏键的情况的输入,需要getline作为输入:这 ...
 - POJ-3669-流星雨
			
这题的话,卡了有两个小时左右,首先更新地图的时候越界了,我们进行更新的时候,要判断一下是不是小于零了,越界就会Runtime Error. 然后bfs 的时候,我没有允许它搜出300以外的范围,然后就 ...
 - activiti工作流使用一般步骤
			
activiti工作流使用的一般步骤 一.在eclipse或Myeclipse中安装activiti插件: 二.通过activiti连接数据库,有以下两种连接数据库的形式: 1.通过java代码链接数 ...
 - Java测试技巧
			
快捷键 修改快捷键 window->preference->general->keys: 查找引用:ctrl+shift+g 覆盖测试:alt+shift+E,T 复制一行:ctrl ...
 - vue源码构建代码分析
			
这是xue源码学习记录,如有错误请指出,谢谢!相互学习相互进步. vue源码目录为 vue ├── src #vue源码 ├── flow #flow定义的数据类型库(vue通过flow来检测数据类型 ...
 - R语言基础-data.frame
			
data.frame比较像表格,每一列是一个向量,即每列中的元素是同一类型:所有列具有相同的长度. x = 10:1 y = -4:5 q = c("Ha","oh&qu ...
 - 牛客网暑期ACM多校训练营(第六场) J  Heritage of skywalkert(数论, eth_element)
			
链接: https://www.nowcoder.com/acm/contest/144/J 题意: 给定一个函数, 求它n次结果中任意两次的lcm最大值 分析: 首先要看出这个函数并没有什么含义, ...