poj1200Crazy Search (哈希)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud
| Time Limit: 1000MS | Memory Limit: 65536K |
Description
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
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
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
题意
给一个字符串,问在该串中总共出现了几种长度为n的子串
哈希。直接搞,最好是双哈希,单哈希不一定能过。
#include <iostream>
#include <sstream>
#include <ios>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <vector>
#include <string>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <climits>
#include <cctype>
using namespace std;
#define XINF INT_MAX
#define INF 0x3FFFFFFF
#define MP(X,Y) make_pair(X,Y)
#define PB(X) push_back(X)
#define REP(X,N) for(int X=0;X<N;X++)
#define REP2(X,L,R) for(int X=L;X<=R;X++)
#define DEP(X,R,L) for(int X=R;X>=L;X--)
#define CLR(A,X) memset(A,X,sizeof(A))
#define IT iterator
typedef long long ll;
typedef pair<int,int> PII;
typedef vector<PII> VII;
typedef vector<int> VI;
const int B=;
char s[B];
bool a[B];
bool c[];
map<char,int>m;
int main()
{
ios::sync_with_stdio(false);
int n,nc;
while(scanf("%d%d",&n,&nc)!=EOF){
scanf("%s",s);
int len=strlen(s);
int b=;
memset(a,,sizeof(a));
for(int i=;i<len;i++)
{
if(c[s[i]-'a']==)c[s[i]-'a']=;
}
int t=;
for(int i=;i<;i++)
{
if(c[i])
{
m[i+'a']=t;
t++;
}
}
b=;
int bl=;
//for(int i=0;i<len;i++)cout<<m[s[i]]<<endl;
for(int i=;i<n;i++)b=(b*nc)%B;
for(int i=;i<n;i++)
{
bl=((bl*nc)%B+m[s[i]])%B;
}
a[bl]=;
for(int i=n;i<len;i++)
{
bl=((bl*nc-m[s[i-n]]*b+B)%B+m[s[i]])%B;
a[bl]=;
}
int ans=;
for(int i=;i<B;i++)
{
if(a[i])ans++;
}
printf("%d\n",ans);
}
return ;
}
代码君
poj1200Crazy Search (哈希)的更多相关文章
- poj1200-Crazy Search(hash入门经典)
Hash:一般是一个整数.就是说通过某种算法,可以把一个字符串"压缩" 成一个整数.一,题意: 给出两个数n,nc,并给出一个由nc种字符组成的字符串.求这个字符串中长度为n的不同 ...
- POJ-1200-Crazy Search(字符串Hash)
Crazy Search Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 33142 Accepted: 9079 Descrip ...
- poj1200Crazy Search(hash)
题目大意 将一个字符串分成长度为N的字串.且不同的字符不会超过NC个.问总共有多少个不同的子串. /* 字符串hash O(n)枚举起点 然后O(1)查询子串hash值 然后O(n)找不一样的个数 ...
- C++-POJ1200-Crazy Search[hash]
由于已经给出字符只有NC种,故可以把子串视为一个NC进制的数,以此构造hash函数就可以了 #include <set> #include <map> #include < ...
- AngularJS——第8章 服务
第8章 服务 服务是一个对象或函数,对外提供特定的功能. 8.1 内建服务 1. $location是对原生Javascript中location对象属性和方法的封装. // $location内置服 ...
- POJ 1200:Crazy Search(哈希)
Crazy Search Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 32483 Accepted: 8947 Des ...
- PAT 甲级 1145 Hashing - Average Search Time (25 分)(读不懂题,也没听说过平方探测法解决哈希冲突。。。感觉题目也有点问题)
1145 Hashing - Average Search Time (25 分) The task of this problem is simple: insert a sequence of ...
- 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 ...
随机推荐
- Mac添加或修改环境变量
方式1. 终端添加或修改 命令:pico, vim等 方式:pico .bash_profile 方式2. 文本方式添加或修改 1)打开 touch ~/.bash_profile open -t ~ ...
- Windows8.1使用博客客户端写博客
1.首先去微软官网下载客户端(Windows live writer) http://windows.microsoft.com/zh-cn/windows-live/essentials 安装步骤 ...
- Bootstrap定制(一)less入门及编译
第一篇博,希望支持. 近期在开发一个项目,项目前端定位于bootstrap,遂花了少许时间研究了bootstrap,将其整理整理,与众人共享. bootstrap官方的定制,功能还算完善,但是基于we ...
- 浅析 JavaScript 组件编写
之前因项目需要也编写过一些简单的JS组件,大多是基于JQuery库的,一直也没有过总结,导致再次写到的时候还去Google, 近日看到一个文章总结的挺好,拿过整理一下做个备忘. 此次同样是基于jque ...
- smarty 常用参数
section的产生是为解决foreach的不足的,与foreach一样,它用于设计模板内的循环块,它较为复杂,可极大程序上满足程序需要,所以在程序中我习惯使用它而不使用foreach,基本原形为:{ ...
- python bottle使用多个端口(多个进程)提高并发
我的程序是用python结合bottle框架写的,但bottle自带wsgi原本只是单进程单线程运行模式(Bottle 默认运行在内置的 wsgiref 服务器上面.这个单线程的 HTTP 服务器在开 ...
- 浅谈 qmake 之 shadow build(将源码路径和构建路径分开,一套源码要分别用msvc2008、msvc2008、mingw分别编译又不互相干扰)
shadow build shadow build 是什么东西?就是将源码路径和构建路径分开(也就是生成的makefile文件和其他产物都不放到源码路径),以此来保证源码路径的清洁. 这不是qmake ...
- 编译型/解释型语言,什么时候用shell
编译型语言 很多传统的程序设计语言,例如Fortran.Ada.Pascal.C.C++和Java,都是编译型语言.这类语言需要预先将我们写好的源代码(source code)转换成目标代码(obje ...
- HDU1698 Just a Hook (区间更新)
Just a Hook Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- 《Java程序员面试笔试宝典》之Java与C/C++有什么异同
Java与C++都是面向对象语言,都使用了面向对象思想(例如封装.继承.多态等),由于面向对象有许多非常好的特性(继承.组合等),使得二者都有很好的可重用性. 需要注意的是,二者并非完全一样,下面主要 ...