转载请注明出处: 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. hdu Intelligent IME

    算法:字典树 题意:手机9键拼音:2:abc  3:def 4:ghi 5:jkl 6:mno 7:pqrs 8:tuv 9:wxyz: 第一行读入一个T,代表测试组数: 之后输入两个整数n和m, 有 ...

  2. 怎样利用putty登陆SSH主机方法

    PuTTY 是一套免费的SSH / Telnet 程序,是在Windows 32平台下的telnet.rlogin和ssh客户端,它是一个跨平台的远程登录工具 下载putty成功后,双击打开Putty ...

  3. Small factorials Solved Problem code: FCTRL2

    import sys def fact(n): final = n while n > 1: final *= n - 1 n -= 1 return final #逻辑严谨,不要忘了retur ...

  4. 文件:一个任务 - 零基础入门学习Python029

    文件:一个任务 让编程改变世界 Change the world by program 一个任务 这节课,我们需要一起来完成一个任务:将文件(record.txt)中的数据进行分割并按照以下规律保存起 ...

  5. 拼接xml时出现内存溢出解决办法

    /// designed by Alex /// 解决内存溢出问题,不可使用迭代器和foreach /// 循环遍历List, 别生成xml,一边释放 /// 反向遍历,相当于出栈,避免索引顺序乱的问 ...

  6. VS2010安装MSDN

    VS2010正式版不再有单独的MSDN Library安装选项,以至于很多同学找不到本地的MSDN Library来用,其实VS2010的ISO安装光盘里已经包含有MSDN Library,只不过要手 ...

  7. AT89C 系列单片机解密原理

    单片机解密简单就是擦除单片机片内的加密锁定位.由于AT89C系列单片机擦除操作时序设计上的不合理.使在擦除片内程序之前首先擦除加密锁定位成为可能.AT89C系列单片机擦除操作的时序为:擦除开始---- ...

  8. openssl对数组加密解密的完整实现代码

    本例是用C实现的对一个数组进行加密,加密到第二个数组,然后解密到另一个数组的完整实现代码. #include <stdio.h> #include <string.h> #in ...

  9. mybatis常用操作

    一.增 1.1 单条 <insert id="addUser" parameterType="com.xxx.model.UserInfo" useGen ...

  10. vijos1781 同余方程

    描述 求关于x的同余方程ax ≡ 1 (mod b)的最小正整数解. 格式 输入格式 输入只有一行,包含两个正整数a, b,用一个空格隔开. 输出格式 输出只有一行,包含一个正整数x0,即最小正整数解 ...