A - Crazy Search

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,nc,并给出一个由nc种字符组成的字符串。求这个字符串中长度为n的子串有多少种。
  先将每个不同字母转化为一个数。再根据此将每一个字串转化为一个数,存入hash表里。存的方法是:每一个字串是一个连续的数,nc(不同字母数)作用就在这里。把这串连续的数转化为nc进制数,然后判断存在就可以了.
  

#include<iostream>          
#include<cstring>
#include<cstdio>
using namespace std;
typedef long long ll;
const int maxn=;
ll hash[maxn],num[];
char s[maxn];
int main()
{
ll n,m;
while(~scanf("%lld%lld%s",&n,&m,s))
{
// memset(hash,0,sizeof(hash));
// memset(num,0,sizeof(num));
ll len=strlen(s);
ll cnt=;
num[s[]]=cnt++;
for(int i=;i<len;i++)
{
if(num[s[i]]==)
num[s[i]]=cnt++;
}
ll ans=;
for(int i=;i<=len-n;i++)
{
ll sum=;
for(int j=;j<n;j++)
{
sum=sum*m+num[s[i+j]];
// printf("%lld * %lld+%lld ",sum,m,num[s[i+j]]);
}
if(!hash[sum])
{
ans++;
// printf("hash[sum]:%lld sum:%lld \n",hash[sum],sum);
hash[sum]=;
}
}
printf("%lld\n",ans);
}
return ;
}
  

POJ1200 A - Crazy Search(哈希)的更多相关文章

  1. [poj1200]Crazy Search(hash)

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

  2. POJ 1200:Crazy Search(哈希)

    Crazy Search Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 32483   Accepted: 8947 Des ...

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

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

  4. poj1200Crazy Search (哈希)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Crazy Search Time Limit: 1000MS   Memory ...

  5. hdu 1381 Crazy Search

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1381 Crazy Search Description Many people like to sol ...

  6. (map string)Crazy Search hdu1381

    Crazy Search Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

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

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

  8. POJ 1200 Crazy Search (哈希)

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

  9. POJ1200 Crazy Search

    Time Limit: 1000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u Description Many peo ...

随机推荐

  1. Maven是什么,如何使用Maven

    一.简单的小问题? 1.1.假如你正在Eclipse下开发两个Java项目,姑且把它们称为A.B,其中A项目中的一些功能依赖于B项目中的某些类,那么如何维系这种依赖关系的呢? 很简单,这不就是跟我们之 ...

  2. jsp el表达式判空

    https://www.cnblogs.com/sxdcgaq8080/p/8119186.html

  3. S7-300过程映像区详解

      一.概念  W过程镜像区输入字 PIW立即输入区字  PIW不用等系统刷新,立即读入 IW等待系统刷新后读入   二.PIW/IW,PQW/QW  引用西门子论坛一位大侠的比方加深理解:      ...

  4. POJ 1027:The Same Game 较(chao)为(ji)复(ma)杂(fan)的模拟

    The Same Game Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5168   Accepted: 1944 Des ...

  5. 编程练习 将一个字符串中的空格替换为 "%20"

    重点:字符串和元组一样, 是不可变对象. 所以将创建一个新的字符串对象,将改变后的字符加入到该新的对象里. 两种方法: 1.python的 replace函数 2.判断修改 def replace(a ...

  6. SQLyog连接报错 Error No.2058 Plugin caching_sha2_password could not be loaded

    参考链接:https://blog.csdn.net/qq_22766431/article/details/80628583 win10系统更新安装Mysql8.0,连接SQLyog的时候出现下面错 ...

  7. 题解:luogu P1247

    大概没你们说得复杂吧...... \(Part\;1\) \(Nim\)游戏 大家都对异或和感到懵逼吧(排除大佬),其实很简单,用\(SG\)函数打表计算即可解决: 抛个板子: void get_sg ...

  8. java执行操作系统脚本

    http://www.cnblogs.com/bencakes/p/6139477.html 以前只是知道Runtime.getRuntime().exec(command);这种用法,但是有时候命令 ...

  9. ACM-寻宝

    题目描述:寻宝 有这么一块神奇的矩形土地,为什么神奇呢?因为上面藏有很多的宝藏.该土地由N*M个小正方形土地格子组成,每个小正方形土地格子上,如果标有“E”,则表示该格可以通过:如果标有“X”,则表示 ...

  10. QObject的timerEvent使用

    其实在QT里,我们自己写的类一般通常需要继承QObject类,因为这一类里规定好的拿来即可用的方法. 比如信号,槽,以及前一个博文写的movetothread方法,这里就是介绍一个QObject的ti ...