转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud

Crazy Search
Time Limit: 1000MS   Memory Limit: 65536K

Description

Many people like to solve hard puzzles some of which may lead them to madness. One such puzzle could be finding a hidden prime number in a given text. Such number could be the number of different substrings of a given size that exist in the text. As you soon will discover, you really need the help of a computer and a good algorithm to solve such a puzzle.
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

The
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

The
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

Huge input,scanf is recommended.

题意

给一个字符串,问在该串中总共出现了几种长度为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 (哈希)的更多相关文章

  1. poj1200-Crazy Search(hash入门经典)

    Hash:一般是一个整数.就是说通过某种算法,可以把一个字符串"压缩" 成一个整数.一,题意: 给出两个数n,nc,并给出一个由nc种字符组成的字符串.求这个字符串中长度为n的不同 ...

  2. POJ-1200-Crazy Search(字符串Hash)

    Crazy Search Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 33142 Accepted: 9079 Descrip ...

  3. poj1200Crazy Search(hash)

    题目大意   将一个字符串分成长度为N的字串.且不同的字符不会超过NC个.问总共有多少个不同的子串. /* 字符串hash O(n)枚举起点 然后O(1)查询子串hash值 然后O(n)找不一样的个数 ...

  4. C++-POJ1200-Crazy Search[hash]

    由于已经给出字符只有NC种,故可以把子串视为一个NC进制的数,以此构造hash函数就可以了 #include <set> #include <map> #include < ...

  5. AngularJS——第8章 服务

    第8章 服务 服务是一个对象或函数,对外提供特定的功能. 8.1 内建服务 1. $location是对原生Javascript中location对象属性和方法的封装. // $location内置服 ...

  6. POJ 1200:Crazy Search(哈希)

    Crazy Search Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 32483   Accepted: 8947 Des ...

  7. PAT 甲级 1145 Hashing - Average Search Time (25 分)(读不懂题,也没听说过平方探测法解决哈希冲突。。。感觉题目也有点问题)

    1145 Hashing - Average Search Time (25 分)   The task of this problem is simple: insert a sequence of ...

  8. Crazy Search POJ - 1200 (字符串哈希hash)

    Many people like to solve hard puzzles some of which may lead them to madness. One such puzzle could ...

  9. POJ 1200 Crazy Search (哈希)

    题目链接 Description Many people like to solve hard puzzles some of which may lead them to madness. One ...

随机推荐

  1. www

    dddd int vec_rotate(char *vec,int rotdist, int length) { int i,j,k,times; char t; times = gcd(rotdis ...

  2. webview 上 postUrl 发送参数过程中数据丢失或错误 的问题

    用到了android 的 webview 来展示页面.webview需要用post来传递参数.于是问题出现了,后台解析中发现参数错误. 之前有因为String 和byte[]转行时,数据丢失的问题,于 ...

  3. mongodb3 权限认证问题总结

    mongodb3 权限认证问题总结 标签(空格分隔): mongodb 权限 数据库 认证 ubuntu用户安装最新版本mongodb 添加key sudo apt-key adv --keyserv ...

  4. python文件_批量改名

    #! /usr/bin/env python #coding=gbk #文件操作实例--将文件夹下所有图片名称加上'_test' import re,os,time #str.split(path) ...

  5. python3连接mysql

    用到模块pymysql,用pip安装 sudo pip3 install pymysql3 编写程序 import pymysql conn = pymysql.connect(host=',db=' ...

  6. 元组:戴上了枷锁的列表 - 零基础入门学习Python013

    元组:戴上了枷锁的列表 让编程改变世界 Change the world by program 元组:戴上了枷锁的列表 由于和列表是近亲关系,所以元组和列表在实际使用上是非常相似的. 我们这节课主要通 ...

  7. 迁移笔记:对ob_start()的总结

    1.Flush:刷新缓冲区的内容,输出. 函数格式:flush() 说明:这个函数经常使用,效率很高. 2.ob_start :打开输出缓冲区 函数格式:void ob_start(void) 说明: ...

  8. POJ 2653 Pick-up sticks(线段相交)

    题意:给定n个木棍依次放下,要求最终判断没被覆盖的木棍是哪些. 思路:快速排斥以及跨立实验可以判断线段相交. #include<algorithm> #include<cstdio& ...

  9. 关于Yeoman使用的总结

    Yeoman由三部分组成 Yo 用于项目构建. Grunt 用于项目管理,任务制定. Bower 用于项目依赖管理. 经过一段时间的使用,对这些东西有了一些个人总结: 总体上说这些内容学习曲线略高,不 ...

  10. ASP.NET MVC3 系列教程 – 新的Layout布局系统

    原文地址:http://www.cnblogs.com/highend/archive/2011/04/18/asp_net_mvc3_layout.html I:回忆MVC2当中MasterPage ...