题目链接

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

分析:

给定一个有nc个不同的字符组成的字符串,然后询问这个字符串里面有多少个长度为n的不完全相同的子串。

首先想到的就是对于这个字符串使用字符串截取函数获取每一个子串,然后利用map来判重。但是这样的话时间会超时,转换一下利用hash的思想来求解。

明确指出是该字符串由nc个不同的字符组成,我们将这nc个字符串对应成nc进制,对应的时候与字符的ASCLL码表没有关系,至于该字符第一次在字符串中出现的顺序有关(当然这个可以根据自己的习惯来定义)

例如题目上给出的:daababac

对应成 4进制后是:01121213

然后根据转换后的进制数,将每一个子串对应成一个一一对应的数字,就可以利用hash在O(1)的时间内进行判重,会大大减少时间。

需要注意的一点就是,因为我们是按照nc进制来求数值的,而不是习惯所有的10进制,说以应该乘上的是nc。

代码:

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
int num[300];
int Hash[16000009];//hash函数
int main()
{
int n,nc;
string str;
while(~scanf("%d%d",&n,&nc))
{
memset(Hash,0,sizeof(Hash));
cin>>str;
int len=str.length();
int cnt=1;
num[str[0]]=0;
for(int i=1; i<len; i++) //将nc个字符转换为对应的nc进制的数,字母和数字是一一对应的
{
if(num[str[i]]==0)//只有当这个字符没有转换过的时候,才将该字符对应成一个数字
{
num[str[i]]=cnt;
cnt++;
}
}
int ans=0,sum;
for(int i=0; i<=len-n; i++)
{
sum=0;
for(int j=i; j<i+n; j++)
{
sum=sum*nc+num[str[j]];//特别要注意这里因为是nc进制的计算所以乘上的是nc,不要因为我们习惯的十进制计算而乘上10
}
//这样每一个长度为n的不同的子串都会唯一的对应一个数字
if(Hash[sum]==0)
{
Hash[sum]=1;
ans++;
}
}
printf("%d\n",ans);
}
return 0;
}

POJ 1200 Crazy Search (哈希)的更多相关文章

  1. poj 1200 Crazy Search(hash)

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

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

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

  3. POJ – 1200 Crazy Search

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

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

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

  5. POJ 1200 Crazy Search

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

  6. POJ 1200 Crazy Search 【hash】

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

  7. POJ 1200 Crazy Search【Hash入门】

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

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

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

  9. poj 1200 crasy search

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

随机推荐

  1. Java之Logger日志(Java8特性)

    import java.util.logging.Level; import java.util.logging.Logger; public class LoggingDemo { public s ...

  2. 小程序源码下载[demo整理自github]

    微信小程序的火热程度大家都有所了解,也有很多牛人写了不错的小程序,今天ytkah就整理一些github上的小程序开源项目,源码可以直接下载来用,感兴趣的朋友赶紧去看看吧!以下小程序排名按star的数量 ...

  3. POJ3176-基础DP

    很基础的dp题.有一头奶牛想接尽量多的苹果,有w此移动机会. dp[i][w] = max(dp[i-1][w+1] + 能否吃到苹果 ,dp[i-1][w] + 能否吃到苹果)  //从上一分钟是否 ...

  4. 【POI每日题解 #7】TES-Intelligence Test

    题目链接 这道题第一眼看去类比BANK-Cash Dispenser 不过1e6 * 1e6 = 1e12   分分钟MLE啊 想到优化 就yy到一种近似主席树的做法 来维护类似BANK的一堆序列 开 ...

  5. 【刷题】AtCoder Regular Contest 003

    A.GPA計算 题意:\(n\) 个人,一个字符串表示每个人的等第,每种等第对应一种分数.问平均分 做法:算 #include<bits/stdc++.h> #define ui unsi ...

  6. 2.9 C++使用默认参数的构造函数

    总结: 默认参数的构造函数,其默认参数必须置于参数列表的结尾. 设计类的构造函数的时候最好不要同时是用构造函数的重载和带参数的构造函数. 我们可以想象一个这样的场景:某一天书店整理库存,发现了一些非常 ...

  7. 【Luogu5108】仰望半月的夜空(后缀数组)

    [Luogu5108]仰望半月的夜空(后缀数组) 题面 洛谷 题解 实名举报这题在比赛之前还不是这个样子的,还被我用SAM给水过去了 很明显求出\(SA\)之后就是按照\(SA\)的顺序从前往后考虑每 ...

  8. 洛谷 P1076 寻宝 解题报告

    P1076 寻宝 题目描述 传说很遥远的藏宝楼顶层藏着诱人的宝藏.小明历尽千辛万苦终于找到传说中的这个藏宝楼,藏宝楼的门口竖着一个木板,上面写有几个大字:寻宝说明书.说明书的内容如下: 藏宝楼共有\( ...

  9. Java: 在不同windows主题下,JFrame窗口设置最佳高度的解决方案

    //设置窗口的大小,无论使用怎样的windows主题,都能灵活的应对,显示合适的窗口大小,一定要在JFrame.setVisible(true)之前调用, //替代传统的frame.setSize(w ...

  10. sscanf,sprintf 通过字符串与其它类型进行方便快捷的输入和输出

    http://c.biancheng.net/cpp/html/296.html 头文件:#include <stdio.h> sscanf()函数用于从字符串中读取指定格式的数据,其原型 ...