Crazy Search

Time Limit: 1000MS Memory Limit: 65536K

Total Submissions: 33142 Accepted: 9079

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

分析

  1. 串的长度最大为16000000,所以如果要用字符串Hash(把字符串当作一个数字)就要尽量使这个数字小。
  2. 利用字符在串中的出现顺序当作该字符的权值,而整个字符串就是nc进制数,然后把数字用hash表储存。
const int p = 131;
char has[16000001];
int a[128];
string str;
int n,nc;
int num = 0,cnt = 0;
int main()
{
cin>>n>>nc;
cin>>str;
int len = str.length(); for(int i=0;i<len;i++)
{
if(!a[str[i]])
a[str[i]] = ++num;
}
for(int i=0;i<len-n+1;i++)
{
int sum=0;
for(int j=i;j<i+n;j++)
sum = sum*nc+a[str[i]];
if(!has[sum])
has[sum] = 1,cnt++;
}
cout<<cnt<<endl;
}

POJ-1200-Crazy Search(字符串Hash)的更多相关文章

  1. POJ 1200 Crazy Search 【hash】

    <题目链接> 题目大意: 给定n,nc,和一个字符串,该字符串由nc种字符组成,现在要你寻找该字符串中长度为n的子字符串有多少种. 解题分析: 因为要判重,所以讲这些字符串hash一下,将 ...

  2. POJ 1200 Crazy Search【Hash入门】

    RK法:https://www.cnblogs.com/16crow/p/6879988.html #include<cstdio> #include<string> #inc ...

  3. POJ 1200 Crazy Search(字符串简单的hash)

    题目:http://poj.org/problem?id=1200 最近看了一个关于hash的问题,不是很明白,于是乎就找了些关于这方面的题目,这道题是一道简单的hash 字符串题目,就先从他入手吧. ...

  4. POJ 1200 Crazy Search 字符串的Hash查找

    第一次涉及HASH查找的知识 对于字符串的查找有很多前人开发出来的HASH函数,比较常用的好像是ELF 和 BKDR. 这道题没想到突破点是在于其nc值,告诉你组成字符串的字母种类. 还有用26进制, ...

  5. poj 1200 Crazy Search(hash)

    题目链接:http://poj.org/problem?id=1200 思路分析:从数据来看,该题目使用线性时间算法,可见子串的比较是不可能的:使用hash可以在常数时间内查找,可以常数时间内判重, ...

  6. POJ 1200 Crazy Search (哈希)

    题目链接 Description Many people like to solve hard puzzles some of which may lead them to madness. One ...

  7. POJ – 1200 Crazy Search

    http://poj.org/problem?id=1200 #include<iostream> #include<cstring> using namespace std; ...

  8. POJ 1200 Crazy Search

    思路:利用Karp-Rabin算法的思想,对每个子串进行Hash,如果Hash值相等则认为这两个子串是相同的(事实上还需要做进一步检查),Karp-Rabin算法的Hash函数有多种形式,但思想都是把 ...

  9. POJ 3461 Oulipo(字符串hash)

    题目链接 字符串hash判断字符串是否相等. code #include<cstdio> #include<algorithm> #include<cstring> ...

  10. poj 1200 crasy search

    https://vjudge.net/problem/POJ-1200 题意: 给出一个字符串,给出子串的长度n和给出的字符串中不同字符的个数nc,统计这个字符串一共有多少不同的长度为n的子串. 思路 ...

随机推荐

  1. 机器学习--DIY笔记与感悟--②决策树(1)

    在完成了K临近之后,今天我们开始下一个算法--->决策树算法. 一.决策树基础知识 如果突然问你"有一个陌生人叫X,Ta今天需要带伞吗?", 你一定会觉得这个问题就像告诉你& ...

  2. hdoj4180

    题意: 使(a/b-c/d)最小,然后让你求c/d. 我们能说最小the error |A/B - C/D| 然后C,D的范围是 0 < C < D < B. 其实就是:求接近(A/ ...

  3. LuoguP2822 组合数问题(组合数,二维前缀和)

    P2822 组合数问题 输入输出样例 输入样例#1: 复制 1 2 3 3 输出样例#1: 复制 1 输入样例#2: 复制 2 5 4 5 6 7 输出样例#2: 复制 0 7 说明 [样例1说明] ...

  4. 关于Page翻页效果, PageViewConrtoller

    Page View Controllers你使用一个page view controller用page by page的方式来展示内容.一个page view controller管理一个self-c ...

  5. Android中R.java丢失不见的解决方案

    最近在做android项目时,遇到报错情况,于是clean了一下,结果java类全都报错了,里面的R下有红线,我一找,发现R.java不见了,真是奇怪,如下图(这是解决以后的): 然后查看了一下Con ...

  6. (二分图最大匹配)51NOD 2006 飞行员配对

    第二次世界大战时期,英国皇家空军从沦陷国征募了大量外籍飞行员.由皇家空军派出的每一架飞机都需要配备在航行技能和语言上能互相配合的2名飞行员,其中1名是英国飞行员,另1名是外籍飞行员.在众多的飞行员中, ...

  7. hdu1598 find the most comfortable road 枚举+最小生成树

    #include<cstdio> #include<cstring> #include<algorithm> #define MAXN 210 #define IN ...

  8. linux虚拟机时间不准的问题

    如果时区不准, 使用tzselect命令(timezone选择),选择北京时间.然后把输出的命令写入/etc/profile.d/time.sh里. 然后用crontab写定时任务,每天执行一次. 3 ...

  9. SPRING-BOOT系列之简介

    来自:51CTO的学习视频,本博客作为一个知识点记录以及代码验证 spring boot 特点 1. 为基于spring的开发提供更快的入门体验 2. 创建可以独立运行的spring应用 3. 直接嵌 ...

  10. 使用json传输数组实例

    client.php <?php //遍历数组元素,并将元素转码 function array_iconv($data, $in_charset='GBK', $out_charset='UTF ...