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. Paragraph 对象'代表所选内容、范围或文档中的一个段落。Paragraph 对象是 Paragraphs 集合的一个成员。Paragraphs 集合包含所选内容、范围或文档中的所有段落。

    Paragraph 对象'代表所选内容.范围或文档中的一个段落.Paragraph 对象是 Paragraphs 集合的一个成员.Paragraphs 集合包含所选内容.范围或文档中的所有段落. 方法 ...

  2. 用Quick Cocos2dx做一个连连看(一)

    呵呵,不知道能不能坚持下来,先写着吧. 预备知识:Quick Cocos2dx 2.2.5基本知识 或者 Cocos2dx基本知识, lua入门 开发工具:Sublime Text 2.0/3.0 原 ...

  3. Unity3d 开发之 ulua 坑的总结

    相同的 lua 代码在安卓上能正常运行,但在 IOS 上可能不会正常运行而导致报红,崩溃等,我在使用 lua 编程时遇到的一些坑总结如下: 1. File.ReadAllText, 诸如以下代码在 i ...

  4. chart.js在html中画曲线图

    http://www.bootcss.com/p/chart.js/docs/ http://www.chartjs.org/docs/   中有详细讲解 一.简介 Chart.js是一个基于HTML ...

  5. mongodb 3.x connect with credential

    package mongoDb; import java.net.UnknownHostException; import java.util.ArrayList; import java.util. ...

  6. iOS开发——设备信息小结(未完待续...)

    1.获取设备的信息  UIDevice *device = [[UIDevice alloc] init]; NSString *name = device.name;       //获取设备所有者 ...

  7. JQuery中 json 和字符串直接相互转换

    json字符串转json对象:jQuery.parseJSON(jsonStr); json对象转json字符串:JSON.stringify(jsonObj);   IE中可能对unicode使用“ ...

  8. UVa 10523 - Very Easy !!!

    题目大意:给你一个公式,直接按照式子计算就可以了,要用到大数. import java.io.*; import java.util.*; import java.math.*; class Main ...

  9. graphical Layout调大一点

    布局最右边的放大器按钮好难找啊

  10. java中的double

    代码如下: Double d1 = 0.35; Double d2 = 0.1; System.out.println(d1+d2); 结果很不幸不是0.45,而是0.4499999999999999 ...