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 ...
随机推荐
- js迭代器模式
在迭代器模式中,通常有一个包含某种数据的集合的对象.该数据可能储存在一个复杂数据结构内部,而要提供一种简单 的方法能够访问数据结构中的每个元素. 实现如下: //迭代器模式 var agg = (fu ...
- 移动web前端的一些硬技能(一)关于移动设备
移动端web其实也就是小尺寸触摸屏设备的web,其中包含了两个词——1.小尺寸设备:2.触摸屏.能把这两个问题给解决掉,就已经是一个基本合格的移动web前端开发工程师了.对于前端工程师来说,前路漫漫, ...
- 一键分享到新浪微博、腾讯微博、搜狐微博、人人网、开心网、百度收藏等js代码大全
下面给大家一些分享的js代码,只要把代码插入自己的网页中稍微修改一下图片路径就可以用了,好了,废话少说,上代码: document.writeln("<b>喜欢本文,那就分享到 ...
- Django的model中日期字段设置默认值的问题
之前写过这样一个model: class MonthlyFeeMember(models.Model): worker = models.ForeignKey(Student, verbose_nam ...
- DataTables选择多行
$(document).ready(function() { var table = $('#example').DataTable(); $('#example tbody').on( 'click ...
- 观《Terminal》之感
读书笔记系列链接地址http://www.cnblogs.com/shoufengwei/p/5714661.html. 经人推荐,用了几天时间欣赏了这部斯皮尔伯格导演的电影<Te ...
- AFNetworking了解
AFNetworking了解 AFNetworking是一个讨人喜欢的网络库,适用于iOS以及Mac OS X. 它构建于在NSURLConnection, NSOperation, 以及其他熟悉 ...
- BZOJ1013 球形空间产生器sphere
Description 有一个球形空间产生器能够在n维空间中产生一个坚硬的球体.现在,你被困在了这个n维球体中,你只知道球面上n+1个点的坐标,你需要以最快的速度确定这个n维球体的球心坐标,以便于摧毁 ...
- 查询反模式 - 正视NULL值
一.提出问题 不可避免地,我们都数据库总有一些字段是没有值的.不管是插入一个不完整的行,还是有些列可以合法地拥有一些无效值.SQL 支持一个特殊的空值,就是NULL. 在很多时候,NULL值导致我们的 ...
- java设计模式--行为型模式--策略模式
策略模式: 策略模式 概述 定义一系列的算法,把它们一个个封装起来,并且使它们可相互替换.本模式使得算法可独立于使用它的客户而变化. 适用性 1.许多相关的类仅仅是行为有异.“策略”提供了一种用多个行 ...