POJ1200 A - Crazy Search(哈希)
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
Output
Sample Input
3 4
daababac
Sample Output
5
Hint
#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(哈希)的更多相关文章
- [poj1200]Crazy Search(hash)
Crazy Search Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 26713 Accepted: 7449 Descrip ...
- POJ 1200:Crazy Search(哈希)
Crazy Search Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 32483 Accepted: 8947 Des ...
- POJ-1200 Crazy Search,人生第一道hash题!
Crazy Search 真是不容易啊,人生第一道hash题竟然是搜博客看题解来的. 题意:给你 ...
- poj1200Crazy Search (哈希)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Crazy Search Time Limit: 1000MS Memory ...
- hdu 1381 Crazy Search
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1381 Crazy Search Description Many people like to sol ...
- (map string)Crazy Search hdu1381
Crazy Search Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- Crazy Search POJ - 1200 (字符串哈希hash)
Many people like to solve hard puzzles some of which may lead them to madness. One such puzzle could ...
- POJ 1200 Crazy Search (哈希)
题目链接 Description Many people like to solve hard puzzles some of which may lead them to madness. One ...
- POJ1200 Crazy Search
Time Limit: 1000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64u Description Many peo ...
随机推荐
- Maven是什么,如何使用Maven
一.简单的小问题? 1.1.假如你正在Eclipse下开发两个Java项目,姑且把它们称为A.B,其中A项目中的一些功能依赖于B项目中的某些类,那么如何维系这种依赖关系的呢? 很简单,这不就是跟我们之 ...
- jsp el表达式判空
https://www.cnblogs.com/sxdcgaq8080/p/8119186.html
- S7-300过程映像区详解
一.概念 W过程镜像区输入字 PIW立即输入区字 PIW不用等系统刷新,立即读入 IW等待系统刷新后读入 二.PIW/IW,PQW/QW 引用西门子论坛一位大侠的比方加深理解: ...
- POJ 1027:The Same Game 较(chao)为(ji)复(ma)杂(fan)的模拟
The Same Game Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 5168 Accepted: 1944 Des ...
- 编程练习 将一个字符串中的空格替换为 "%20"
重点:字符串和元组一样, 是不可变对象. 所以将创建一个新的字符串对象,将改变后的字符加入到该新的对象里. 两种方法: 1.python的 replace函数 2.判断修改 def replace(a ...
- 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的时候出现下面错 ...
- 题解:luogu P1247
大概没你们说得复杂吧...... \(Part\;1\) \(Nim\)游戏 大家都对异或和感到懵逼吧(排除大佬),其实很简单,用\(SG\)函数打表计算即可解决: 抛个板子: void get_sg ...
- java执行操作系统脚本
http://www.cnblogs.com/bencakes/p/6139477.html 以前只是知道Runtime.getRuntime().exec(command);这种用法,但是有时候命令 ...
- ACM-寻宝
题目描述:寻宝 有这么一块神奇的矩形土地,为什么神奇呢?因为上面藏有很多的宝藏.该土地由N*M个小正方形土地格子组成,每个小正方形土地格子上,如果标有“E”,则表示该格可以通过:如果标有“X”,则表示 ...
- QObject的timerEvent使用
其实在QT里,我们自己写的类一般通常需要继承QObject类,因为这一类里规定好的拿来即可用的方法. 比如信号,槽,以及前一个博文写的movetothread方法,这里就是介绍一个QObject的ti ...