转载请注明出处: 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. mongodb3 权限认证问题总结

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

  2. DataTables选择多行

    $(document).ready(function() { var table = $('#example').DataTable(); $('#example tbody').on( 'click ...

  3. http、TCP/IP协议与socket之间的区别

    http.TCP/IP协议与socket之间的区别     网络由下往上分为:  www.2cto.com   物理层--                       数据链路层-- 网络层--   ...

  4. Qt编程之信号与槽-------unresolved external symbol "public: virtual struct QMetaObject const * __thiscall XX::metaObject(void)const

    原因是加入Q_OBJECT这个macro的类,被编译的时候就要用到moc这个命令,所以在VS2010中,没有加入此命令的应用,当然会出错了.所以解决办法是加,或者如果你不使用信号槽可以直接删除. 当要 ...

  5. 一次处理ORA-07445的历险记(转)

    ORA-07445通常是Oracle调用操作系统的资源出错时出现的[@more@] 事前没有任何征兆,下午5点左右某个关键应用的17台oracle数据库上的数据库实例陆续宕机,赶紧查看alert_lo ...

  6. eclipse php 开发环境配置

    一般常用的是eclipse+pdt.我是直接下载的Eclipse for php :http://www.eclipse.org/downloads/packages/eclipse-php-deve ...

  7. logstash date插件介绍

    时间处理(Date) 之前章节已经提过, filters/date 插件可以用来转换你的日志记录中的时间字符串,变成 LogStash::Timestamp 对象,然后转存到 @timestamp 字 ...

  8. EBS R12 查询EBS用户相关SQL

    --R12查询EBS在线用户SQL  SELECT U.USER_NAME,        APP.APPLICATION_SHORT_NAME,        FAT.APPLICATION_NAM ...

  9. spring3 jsp页面使用<form:form modelAttribute="xxxx" action="xxxx">报错,附连接数据库的spring MVC annotation 案例

    在写一个使用spring3 的form标签的例子时,一直报错,错误信息为:java.lang.IllegalStateException: Neither BindingResult nor plai ...

  10. 关于sem_unlink什么时候删除信号量

    sem_unlink在man手册里有这么一段话: sem_unlink() removes the named semaphore referred to by name. The semaphore ...