Crazy Search

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 27536   Accepted: 7692

Description

Many people like to solve hard puzzles some of which may lead them to madness. One such puzzle could be finding a hidden prime number in a given text. Such number could be the number of different substrings of a given size that exist in the text. As you soon will discover, you really need the help of a computer and a good algorithm to solve such a puzzle. 
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

The first line of input consists of two numbers, N and NC, separated by exactly one space. This is followed by the text where the search takes place. You may assume that the maximum number of substrings formed by the possible set of characters does not exceed 16 Millions.

Output

The program should output just an integer corresponding to the number of different substrings of size N found in the given text.

Sample Input

3 4
daababac

Sample Output

5

Hint

Huge input,scanf is recommended.

思路:把长度为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)的更多相关文章

  1. [poj1200]Crazy Search(hash)

    Crazy Search Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 26713 Accepted: 7449 Descrip ...

  2. poj1200 字符串hash 滚动哈希初探

    假如要判断字符串A“AABA”是否是字符串B“AABAACAADAABAABA”的子串 最朴素的算法是枚举B的所有长度为4的子串,然后逐个与A进行对比,这样的时间复杂度是O(mn),m为A的长度,n为 ...

  3. POJ-1200 Crazy Search,人生第一道hash题!

                                                        Crazy Search 真是不容易啊,人生第一道hash题竟然是搜博客看题解来的. 题意:给你 ...

  4. HDOJ--4821--String【弦hash】

    联系:http://acm.hdu.edu.cn/showproblem.php?pid=4821 题意:给一个字符串,选m个长度为l的子串组成新的串.要求这m个子串互不同样,问有多少种组合. 字符串 ...

  5. 复杂的 Hash 函数组合有意义吗?

    很久以前看到一篇文章,讲某个大网站储存用户口令时,会经过十分复杂的处理.怎么个复杂记不得了,大概就是先 Hash,结果加上一些特殊字符再 Hash,结果再加上些字符.再倒序.再怎么怎么的.再 Hash ...

  6. 对抗密码破解 —— Web 前端慢 Hash

    (更新:https://www.cnblogs.com/index-html/p/frontend_kdf.html ) 0x00 前言 天下武功,唯快不破.但在密码学中则不同.算法越快,越容易破. ...

  7. 散列表(hash table)——算法导论(13)

    1. 引言 许多应用都需要动态集合结构,它至少需要支持Insert,search和delete字典操作.散列表(hash table)是实现字典操作的一种有效的数据结构. 2. 直接寻址表 在介绍散列 ...

  8. hash表长度优化证明

    hash表冲突的解决方法一般有两个方向: 一个是倾向于空间换时间,使用向量加链表可以最大程度的在节省空间的前提下解决冲突. 另外一个倾向于时间换空间,下面是关于这种思路的一种合适表长度的证明过程: 这 ...

  9. SQL Server-聚焦查询计划Stream Aggregate VS Hash Match Aggregate(二十)

    前言 之前系列中在查询计划中一直出现Stream Aggregate,当时也只是做了基本了解,对于查询计划中出现的操作,我们都需要去详细研究下,只有这样才能对查询计划执行的每一步操作都了如指掌,所以才 ...

随机推荐

  1. ActiveMQ讯息传送机制以及ACK机制

    http://blog.csdn.net/lulongzhou_llz/article/details/42270113 ActiveMQ消息传送机制以及ACK机制详解 AcitveMQ是作为一种消息 ...

  2. JavaScript------for-in的使用方法

    1.在数组中使用 var str = ["a", "b", "c"]; for (var item in str) { //alert(it ...

  3. java 单元测试 注入

    ApplicationContext appContext = new ClassPathXmlApplicationContext("appContext.xml"); MySQ ...

  4. scp命令和sftp命令

    scp帮助命令: man scpscp功能:下载远程文件或者目录到本地,如果想上传或者想下载目录,最好的办法是采用tar压缩一下,是最明智的选择.从远程主机下载东西到本地电脑拷贝文件命令 scp us ...

  5. (简单) POJ 3087 Shuffle'm Up,枚举。

    Description A common pastime for poker players at a poker table is to shuffle stacks of chips. Shuff ...

  6. Mac上ssh无法登录的问题

    今天起来发现阿里云ssh无法登录了 出现一条警告: WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! 开始打算清理know_hosts发现失败,清了根本 ...

  7. ServerSocketChannel

    Java NIO 中的 ServerSocketChannel 是一个可以监听新进来的 TCP 连接的通道, 就像标准 IO 中的 ServerSocket 一样.ServerSocketChanne ...

  8. tornado 增加日志模块

    log类 #!/usr/bin/env python # -*- coding:utf-8 -*- import logging.handlers class Logger(logging.Logge ...

  9. phpcms的网页替换

    //替换首页header:loge里面的首页不用替换<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ...

  10. Xcode各版本官方下载

    官方下载, 用开发者账户登录,建议用Safari浏览器下载. 官方下载地址: https://developer.apple.com/xcode/downloads/ Xcode 66.4: http ...