C++-POJ1200-Crazy Search[hash]
由于已经给出字符只有NC种,故可以把子串视为一个NC进制的数,以此构造hash函数就可以了
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int MAXN=;
char s[MAXN];int hash[MAXN],num[];
int main(){
for(int N,NC;scanf("%d%d",&N,&NC)!=EOF;){
memset(hash,,sizeof(hash));
memset(num,,sizeof(num));
scanf("%s",s+);
int cnt=,ans=,len=strlen(s+);
for(int i=;i<=len&&cnt<=NC;i++)if(!num[s[i]])num[s[i]]=cnt++;
for(int i=;i<=len-N+;i++){
int hashi=;for(int j=i;j<=i+N-;j++)hashi=hashi*NC+num[s[j]];
hash[hashi]?:ans++,hash[hashi]=;
}
cout<<ans<<endl;
}
return ;
}
C++-POJ1200-Crazy Search[hash]的更多相关文章
- [poj1200]Crazy Search(hash)
Crazy Search Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 26713 Accepted: 7449 Descrip ...
- POJ-1200 Crazy Search,人生第一道hash题!
Crazy Search 真是不容易啊,人生第一道hash题竟然是搜博客看题解来的. 题意:给你 ...
- POJ1200 Crazy Search
Time Limit: 1000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64u Description Many peo ...
- poj 1200 Crazy Search(hash)
题目链接:http://poj.org/problem?id=1200 思路分析:从数据来看,该题目使用线性时间算法,可见子串的比较是不可能的:使用hash可以在常数时间内查找,可以常数时间内判重, ...
- hdu1381 Crazy Search(hash map)
题目意思: 给出一个字符串和字串的长度,求出该字符串的全部给定长度的字串的个数(不同样). 题目分析: 此题为简单的字符串哈hash map问题,能够直接调用STL里的map类. map<str ...
- POJ1200 A - Crazy Search(哈希)
A - Crazy Search Many people like to solve hard puzzles some of which may lead them to madness. One ...
- 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 ...
- POJ 1200:Crazy Search(哈希)
Crazy Search Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 32483 Accepted: 8947 Des ...
- Crazy Search POJ - 1200 (字符串哈希hash)
Many people like to solve hard puzzles some of which may lead them to madness. One such puzzle could ...
随机推荐
- jQuery笔记(六)jQuery之Ajax
jQuery确实是一个挺好的轻量级的JS框架,能帮助我们快速的开发JS应用,并在一定程度上改变了我们写JavaScript代码的习惯. 废话少说,直接进入正题,我们先来看一些简单的方法,这些方法都是对 ...
- 【14】Softmax回归
在下面的内容中,我们用C来表示需要分的类数. 最后一层的隐藏单元个数为4,为所分的类的数目,输出的值表示属于每个类的概率. Softmax函数的具体步骤如下图: 简单来说有三步: 计算z值(4×1矩阵 ...
- Hadoop学习之路(5)Mapreduce程序完成wordcount
程序使用的测试文本数据: Dear River Dear River Bear Spark Car Dear Car Bear Car Dear Car River Car Spark Spark D ...
- mp3格式音频 不能立即播放
原因是mp3的meta信息在mp3文件后面,所以要都加载完才能播放 而m4a 格式的 可以立即播放
- 《NVM-Express-1_4-2019.06.10-Ratified》学习笔记(4.12-加-6.2-加-7.2.5.2)Fused_Operations
4.12 Fused Operations 融合操作 融合操作通过“fusing”把两个简单的命令融合一起来支持一个更复杂的命令.协议规定这个特性是可选的:如果支持此特性,需要在Figure 247 ...
- LED Mood Light Factory-Smart Mood Light: Control System Principle
Intelligent devices have become more and more widespread in our lives. Intelligent scene lights are ...
- os::commit_memory(0x0000000085330000, 2060255232, 0) failed; error='Cannot allocate memory' (errno=12)
centos 安装 elasticsearch的时候 因为 elasticsearch默认 需要 2G内存导致的镜像不能运行 解决方案 修改配置文件 find / -name jvm.options ...
- web 项目添加 x86 的dll 引用,模块 DLL c:\WINDOWS\system32\inetsrv\aspnetcore.dll 未能加载。
最近的项目要添加一个 x86 编译的dll, 首先添加引用,编译,报错: 首先判断是 项目中不能添加 x86 的引用,所以把所有的项目都按照 x86 的方式编译一遍,同时对应IIS 的应用池,也修改为 ...
- Alan Walker MV 合辑01 by defender
Alan Walker MV合辑 出来啦! 百度网盘下载地址: 链接:https://pan.baidu.com/s/10WSool70XBe_8tJOae8V-w 提取码:uckq 地址查看 Mi ...
- SDOI2010 粟粟的书架 lg2468(可持久化,前缀和)
题面见https://www.luogu.org/problemnew/show/P2468 然后这道题属于合二为一题,看一眼数据范围就能发现 首先我们先考虑50分,二维前缀和维护一下(反正我不记得公 ...