题目描述

如题,给定N个字符串(第i个字符串长度为Mi,字符串内包含数字、大小写字母,大小写敏感),请求出N个字符串中共有多少个不同的字符串。

友情提醒:如果真的想好好练习哈希的话,请自觉,否则请右转PJ试炼场:)

输入输出格式

输入格式:

第一行包含一个整数N,为字符串的个数。

接下来N行每行包含一个字符串,为所提供的字符串。

输出格式:

输出包含一行,包含一个整数,为不同的字符串个数。

输入输出样例

输入样例#1:

5
abc
aaaa
abc
abcc
12345
输出样例#1:

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的更多相关文章

  1. Crazy Search POJ - 1200 (字符串哈希hash)

    Many people like to solve hard puzzles some of which may lead them to madness. One such puzzle could ...

  2. 牛客练习赛33 E tokitsukaze and Similar String (字符串哈希hash)

    链接:https://ac.nowcoder.com/acm/contest/308/E 来源:牛客网 tokitsukaze and Similar String 时间限制:C/C++ 2秒,其他语 ...

  3. luoguP3370 【模板】字符串哈希 [hash]

    题目描述 如题,给定N个字符串(第i个字符串长度为Mi,字符串内包含数字.大小写字母,大小写敏感),请求出N个字符串中共有多少个不同的字符串. 友情提醒:如果真的想好好练习哈希的话,请自觉,否则请右转 ...

  4. 从Hash Killer I、II、III论字符串哈希

    首先,Hash Killer I.II.III是BZOJ上面三道很经典的字符串哈希破解题.当时关于II,本人还琢磨了好久,但一直不明白为啥别人AC的代码都才0.3kb左右,直到CYG神犇说可以直接随机 ...

  5. HASH 字符串哈希 映射转化

    哈希HASH的本质思想类似于映射.离散化. 哈希,通过给不同字符赋不同的值.并且钦定一个进制K和模数,从而实现一个字符串到一个模意义下的K进制数上. 它的主要目的是判重,用于$DFS$.$BFS$判重 ...

  6. Redis支持的数据类型及相应操作命令:String(字符串),Hash(哈希),List(列表),Set(集合)及zset(sorted set:有序集合)

    help 命令,3种形式: help 命令 形式 help @<group> 比如:help @generic.help @string.help @hash.help @list.hel ...

  7. Redis 命令,键(key),字符串(String),哈希(Hash),列表(List),集合(Set)(二)

      Redis 命令 Redis 命令用于在 redis 服务上执行操作. 要在 redis 服务上执行命令需要一个 redis 客户端.Redis 客户端在我们之前下载的的 redis 的安装包中. ...

  8. 【基本算法入门-字符串哈希(Hash)】-C++

    字符串哈希入门 说得通俗一点,字符串哈希实质上就是把每个不同的字符串转成不同的整数. 为什么会有这样的需要呢?很明显,存储一个超长的字符串和存储一个超大但是能存的下的整数,后者所占的空间会少的多,但主 ...

  9. HDU 1880 魔咒词典(字符串哈希)

    题目链接 Problem Description 哈利波特在魔法学校的必修课之一就是学习魔咒.据说魔法世界有100000种不同的魔咒,哈利很难全部记住,但是为了对抗强敌,他必须在危急时刻能够调用任何一 ...

随机推荐

  1. 洛谷 P1032 字串变换 (BFS)

    题目传送门 我即使是死了,钉在棺材里了,也要在墓里,用这腐朽的声带喊出 STL大法好 这题最麻烦的其实是处理字符串,真正的搜索部分我个人认为也就只有橙题或黄题的难度.而处理字符串,正如前面所说,STL ...

  2. Protocol(协议)、Delegate(委托)、DataSource(数据源)

    这里以 UITableViewController 和 UITableView 的关系为例: //--------------------------------------------------- ...

  3. 基于Vue+VueRouter+ModJS+Fis3快速搭建H5项目总结

    技术选型 • 框架 - Vue+VueRouter • 相比较于react/angular/avalon ? • 简单轻量,社区配套完整• 模块化 - ModJS • 相比较于require/seaj ...

  4. xadmin下设置“use_bootswatch = True”无效的解决办法

    环境: python 2.7 django 1.9 xadmin采用源代码的方式引入到项目中 问题: 在xadmin使用的过程中,设置“use_bootswatch = True”,企图调出主题菜单, ...

  5. 更改ubuntu的官方镜像源

    我们自己安装的ubuntu通常默认镜像源是官方的,并不好用,因为网速以及限制比较多,所以为了使用方便,通常都会去更改一下默认的镜像源配置. 这里我们使用清华大学开源镜像软件站,https://mirr ...

  6. 用xtrabackup实现mysql的主从复制 阿里云rds到自己创建mysql

    来源 http://blog.51cto.com/825536458/1803968参考https://segmentfault.com/a/1190000003063874 如果我们用传统的mysq ...

  7. 编译openwrt_MT7688_hiwooya

    参考链接: 无涯论坛地址: http://www.hi-wooya.com/forum.php openwrt官网地址:https://openwrt.org/zh-cn/doc/howto/buil ...

  8. python--MySQL权限管理 数据备份还原

    一 权限管理 mysql最高管理者是root用户, 这个一般掌握在公司DBA手里, 当你想去对数据库进行一些操作的时候,需要DBA授权给你. 1. 对新用户增删改 1. 创建用户 # 要先use my ...

  9. 用java Graphics生成验证码

    以下下是API文档对Graphics的介绍! Graphics 类是所有图形上下文的抽象基类,允许应用程序在组件(已经在各种设备上实现)以及闭屏图像上进行绘制. Graphics 对象封装了 Java ...

  10. 【练习】reserving.kr之easy ELF

    打开主函数: int __cdecl main() { int result; // eax@2 write(, "Reversing.Kr Easy ELF\n\n", 0x17 ...