POJ1200(hash)
Crazy Search
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 27536 | Accepted: 7692 |
Description
Your task is to write a program that given the size, N, of the substring, the number of different characters that may occur in the text, NC, and the text itself, determines the number of different substrings of size N that appear in the text.
As an example, consider N=3, NC=4 and the text "daababac". The different substrings of size 3 that can be found in this text are: "daa"; "aab"; "aba"; "bab"; "bac". Therefore, the answer should be 5.
Input
Output
Sample Input
3 4
daababac
Sample Output
5
Hint
思路:把长度为n的子串hash成nc进制数,避免了冲突。
//2016.9.4
#include <iostream>
#include <cstdio>
#include <cstring>
#define N 16000005 using namespace std; char str[N];
bool Hash[N];
int id[]; int main()
{
int n, nc, ans, cnt;
while(scanf("%d%d", &n, &nc)!=EOF)
{
cnt = ans = ;
memset(Hash, false, sizeof(Hash));
memset(id, -, sizeof(id));
scanf("%s", str);
int len = strlen(str);
for(int i = ; i < len && cnt < nc; i++)//对str出现的字符进行编号,使之转换成数字
{
if(id[str[i]] != -)continue;
id[str[i]] = cnt++;
}
for(int i = ; i < len-n+; i++)//把长度为n的子串hash成nc进制数
{
int tmp = ;
for(int j = i; j < i+n; j++)
tmp = tmp*nc+id[str[j]];
if(Hash[tmp])continue;
ans++;
Hash[tmp] = true;
}
printf("%d\n", ans);
} return ;
}
POJ1200(hash)的更多相关文章
- [poj1200]Crazy Search(hash)
Crazy Search Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 26713 Accepted: 7449 Descrip ...
- poj1200 字符串hash 滚动哈希初探
假如要判断字符串A“AABA”是否是字符串B“AABAACAADAABAABA”的子串 最朴素的算法是枚举B的所有长度为4的子串,然后逐个与A进行对比,这样的时间复杂度是O(mn),m为A的长度,n为 ...
- POJ-1200 Crazy Search,人生第一道hash题!
Crazy Search 真是不容易啊,人生第一道hash题竟然是搜博客看题解来的. 题意:给你 ...
- HDOJ--4821--String【弦hash】
联系:http://acm.hdu.edu.cn/showproblem.php?pid=4821 题意:给一个字符串,选m个长度为l的子串组成新的串.要求这m个子串互不同样,问有多少种组合. 字符串 ...
- 复杂的 Hash 函数组合有意义吗?
很久以前看到一篇文章,讲某个大网站储存用户口令时,会经过十分复杂的处理.怎么个复杂记不得了,大概就是先 Hash,结果加上一些特殊字符再 Hash,结果再加上些字符.再倒序.再怎么怎么的.再 Hash ...
- 对抗密码破解 —— Web 前端慢 Hash
(更新:https://www.cnblogs.com/index-html/p/frontend_kdf.html ) 0x00 前言 天下武功,唯快不破.但在密码学中则不同.算法越快,越容易破. ...
- 散列表(hash table)——算法导论(13)
1. 引言 许多应用都需要动态集合结构,它至少需要支持Insert,search和delete字典操作.散列表(hash table)是实现字典操作的一种有效的数据结构. 2. 直接寻址表 在介绍散列 ...
- hash表长度优化证明
hash表冲突的解决方法一般有两个方向: 一个是倾向于空间换时间,使用向量加链表可以最大程度的在节省空间的前提下解决冲突. 另外一个倾向于时间换空间,下面是关于这种思路的一种合适表长度的证明过程: 这 ...
- SQL Server-聚焦查询计划Stream Aggregate VS Hash Match Aggregate(二十)
前言 之前系列中在查询计划中一直出现Stream Aggregate,当时也只是做了基本了解,对于查询计划中出现的操作,我们都需要去详细研究下,只有这样才能对查询计划执行的每一步操作都了如指掌,所以才 ...
随机推荐
- office web apps部署(一)
准备两台服务器,A服务器作为域控制器,B服务器安装owa 1.配置域服务器A 参考 2.B服务器加入A所创建的域 参考 3.B服务器安装owa 参考 注意: 1.B服务器加入域后,使用A服务器的管理员 ...
- Spring mvc 返回json格式 - 龙企阁 - 博客频道 - CSDN.NET
第一次使用spring mvc ,在此也算是记录一下以防忘记,希望有经验的朋友指出不足的地方 一.使用maven管理jar. <dependency> <groupId>org ...
- js 如何动态添加数组_百度知道
1.数组的创建var arrayObj = new Array(); //创建一个数组var arrayObj = new Array([size]); //创建一个数组并指定长度,注意不是上限,是长 ...
- jquery如何判断元素是否被点击_百度知道
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 <div id="parent"> <a id="a1" ...
- openstack controller ha测试环境搭建记录(三)——配置haproxy
haproxy.cfg请备份再编辑:# vi /etc/haproxy/haproxy.cfg global chroot /var/lib/haproxy daemon group ...
- Java笔记(二)
10. public protected default private 同一个类中 √ √ √ √ 同一个包中 √ √ √ 子类 √ √ 不同包中 √ 11. 线程: s ...
- jquery.validate.js 一个jQuery验证格式控件
官网地址:http://bassistance.de/jquery-plugins/jquery-plugin-validation jQuery plugin: Validation 使用说明 转载 ...
- Ubuntu Apache2 配置解析
转自:http://www.cnblogs.com/ylan2009/archive/2012/02/25/2368028.html Ubuntu的Apache 2.4 之后的版本的配置文件是 / ...
- WKWebKit基础
UIWebView & UIWebViewDelegate 这个两个东西在 WKWebKit 中被重构成 14 个类 3 个协议. WKWebKit Framework Classes WKB ...
- vim中c/c++源码跳转
在使用vim阅读c/c++代码的时候,代码跳转很重要, 在学习redis代码的时候遇到这个问题. 网上查找之后通过实践发现cscope比较好用,可以很方便的实现跳转 1. 安装cscope sudo ...