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 ...
随机推荐
- 本地连接 HDFS 报错 Exception in thread "main" org.apache.hadoop.security.AccessControlException: org.apache.hadoop.security.AccessControlException: Permission denied: user=JM.H,access=WRITE, inode="":r
此时 到hdfs机器下修改hdfs-site.xml即可 添加如下配置 <property> <name>dfs.permissions</name> <va ...
- mac安装扩展出现grep: /usr/include/php/main/php.h
在Mac下执行 sudo phpize时提示: grep: /usr/include/php/main/php.h: No such file or directorygrep: /usr/inclu ...
- 选择本地文件上传控件 input标签
当要通过控件来选择本地文件上传的时候的一种方式 <input type="file" id="input-file"/> 注意 type类型一定要是 ...
- PHP截取指定字符前的字符串
$str = 'A|B||C|D'; echo substr($str,0,strpos($str, '||')); 输出:A|B
- python 关于异常处理 try...except... 的两个案例
输入若干个成绩,求所有成绩的平均分.每输入一个成绩后询问是否继续输入下一个成绩,回答“yes”就继续输入下一个成绩,回答“no”就停止输入成绩. numbers = [] #使用列表存放临时数据 wh ...
- vue的MVVM
Vue的相关知识有 字符串模板 MVVM 虚拟dom和domdiff,查看下一篇笔记 字符串模板 function render(template, data) { const reg = /\{\{ ...
- Tcp 3次握手 4次挥手
Tcp 3次握手 4次挥手 标签(空格分隔): Java基础 报文介绍: SYN(synchronous建立联机) ACK(acknowledgement 确认) FIN(finish结束) PSH( ...
- 006-PHP检测是否为整数
<?php function checkInteger($Number) { if ($Number > 1) { /* 整数减1仍然是整数 */ return (checkInteger ...
- 树莓派 Raspberry 软件源更改 看门狗启用
看门狗无法在pi1上执行,似乎后更高级的pi上面才可用 1.替换脚本 下面脚本请直接复制到终端执行!! 适用于raspbian-stretch(基于Debian9) sudo -s echo -e & ...
- --转载 详细介绍java中的数据结构
http://developer.51cto.com/art/201107/273003.htm 也许你已经熟练使用了java.util包里面的各种数据结构,但是我还是要说一说java版数据结构与算法 ...