RK法:https://www.cnblogs.com/16crow/p/6879988.html

#include<cstdio>
#include<string>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<cstring>
#include<set>
#include<queue>
#include<algorithm>
#include<vector>
#include<map>
#include<cctype>
#include<stack>
#include<sstream>
#include<list>
#include<assert.h>
#include<bitset>
#include<numeric>
#define debug() puts("++++")
#define gcd(a,b) __gcd(a,b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a,b,sizeof(a))
#define sz size()
#define be begin()
#define mp make_pair
#define pu push_up
#define pd push_down
#define cl clear()
#define lowbit(x) -x&x
#define all 1,n,1
#define rep(i,x,n) for(int i=(x); i<=(n); i++)
#define in freopen("in.in","r",stdin)
#define out freopen("out.out","w",stdout)
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> P;
const ULL base = ;//
const int INF = 0x3f3f3f3f;
const LL LNF = 1e18;
const int maxn = +;
const int maxm = 1e6 + ;
const double PI = acos(-1.0);
const double eps = 1e-;
const int dx[] = {-,,,,,,-,-};
const int dy[] = {,,,-,,-,,-};
int dir[][] = {{,},{,-},{-,},{,}};
const int mon[] = {, , , , , , , , , , , , };
const int monn[] = {, , , , , , , , , , , , };
int t,n,m;
char s[maxn];
int has[maxn],a[];
int main()
{
while(~scanf("%d%d",&n,&m))//将字符串对应到m进制数
{
set<int> st;
ms(a,),ms(has,);
int cnt=,sum=,num=;
scanf("%s",s);
int len = strlen(s);
//按照字符出现的先后顺序确定字符的大小
for(int i=;i<len;i++)//根据字符出现的顺序,给字符标号1,2...
{
if(!a[s[i]])
a[s[i]]=++num;
} for(int i=;i<len;i++)
printf("a[%d] = %d\n",s[i],a[s[i]]); for(int i=;i+n-<len;i++) //把子串映射到hash数组中
{
sum=;
for(int j=i;j<i+n;j++)//子串长度
{
sum=sum*m+a[s[j]]; //将长度为n的子串看作是n位m进制数,这里求的是这个n位m进制数的对应的十进制数
printf("s[%d]=%d a[s[j]]=a[%d]=%d sum=%d\n",j,s[j],s[j],a[s[j]],sum); }
//st.insert(sum);
printf("SUM = %d\n",sum);
if(!has[sum])//has[sum]==0表示没有没有出现过
{
cnt++; //不同串的个数加1
has[sum]=;
}
}
printf("cnt = %d\n",cnt);
}
}
/*
【题意】
把出现过的每个字母映射到对应的数字,这样字符串就变成相应的m进制数,然后把它转换成10进制,并放入has[]中,如果是第一次放入,则总数加一
n m len=8
3 4
daababac
a[d] = 1
a[a] = 2
a[b] = 3
a[c] = 4
daa=122(4进制)
转换为十进制: daa = 1 * 4 ^ 2 + 2 * 4 ^ 1 + 2 * 4 ^ 0 = 16+8+2=26
然后if(!hash[26]) hash[26]++; 【类型】 【分析】 【时间复杂度&&优化】 【trick】
对字符串hash的优化就是,重新定义各个字母的编号,而不是直接用ASCII码值 【数据】
3 4
daababac
a[100] = 1
a[97] = 2
a[97] = 2
a[98] = 3
a[97] = 2
a[98] = 3
a[97] = 2
a[99] = 4
s[0]=100 a[s[j]]=a[100]=1 sum=1
s[1]=97 a[s[j]]=a[97]=2 sum=6
s[2]=97 a[s[j]]=a[97]=2 sum=26
SUM = 26
s[1]=97 a[s[j]]=a[97]=2 sum=2
s[2]=97 a[s[j]]=a[97]=2 sum=10
s[3]=98 a[s[j]]=a[98]=3 sum=43
SUM = 43
s[2]=97 a[s[j]]=a[97]=2 sum=2
s[3]=98 a[s[j]]=a[98]=3 sum=11
s[4]=97 a[s[j]]=a[97]=2 sum=46
SUM = 46
s[3]=98 a[s[j]]=a[98]=3 sum=3
s[4]=97 a[s[j]]=a[97]=2 sum=14
s[5]=98 a[s[j]]=a[98]=3 sum=59
SUM = 59
s[4]=97 a[s[j]]=a[97]=2 sum=2
s[5]=98 a[s[j]]=a[98]=3 sum=11
s[6]=97 a[s[j]]=a[97]=2 sum=46
SUM = 46
s[5]=98 a[s[j]]=a[98]=3 sum=3
s[6]=97 a[s[j]]=a[97]=2 sum=14
s[7]=99 a[s[j]]=a[99]=4 sum=60
SUM = 60
cnt = 5 */

POJ 1200 Crazy Search【Hash入门】的更多相关文章

  1. poj 1200 Crazy Search(hash)

    题目链接:http://poj.org/problem?id=1200 思路分析:从数据来看,该题目使用线性时间算法,可见子串的比较是不可能的:使用hash可以在常数时间内查找,可以常数时间内判重, ...

  2. POJ 1200 Crazy Search(字符串简单的hash)

    题目:http://poj.org/problem?id=1200 最近看了一个关于hash的问题,不是很明白,于是乎就找了些关于这方面的题目,这道题是一道简单的hash 字符串题目,就先从他入手吧. ...

  3. POJ 1200 Crazy Search 【hash】

    <题目链接> 题目大意: 给定n,nc,和一个字符串,该字符串由nc种字符组成,现在要你寻找该字符串中长度为n的子字符串有多少种. 解题分析: 因为要判重,所以讲这些字符串hash一下,将 ...

  4. POJ 1200 Crazy Search 字符串的Hash查找

    第一次涉及HASH查找的知识 对于字符串的查找有很多前人开发出来的HASH函数,比较常用的好像是ELF 和 BKDR. 这道题没想到突破点是在于其nc值,告诉你组成字符串的字母种类. 还有用26进制, ...

  5. POJ 1200 Crazy Search (哈希)

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

  6. POJ – 1200 Crazy Search

    http://poj.org/problem?id=1200 #include<iostream> #include<cstring> using namespace std; ...

  7. POJ 1200 Crazy Search

    思路:利用Karp-Rabin算法的思想,对每个子串进行Hash,如果Hash值相等则认为这两个子串是相同的(事实上还需要做进一步检查),Karp-Rabin算法的Hash函数有多种形式,但思想都是把 ...

  8. [poj1200]Crazy Search(hash)

    Crazy Search Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 26713 Accepted: 7449 Descrip ...

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

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

随机推荐

  1. [洛谷P3338] [ZJOI2014]力

    洛谷题目链接:P3338 [ZJOI2014]力 题目描述 给出n个数qi,给出Fj的定义如下: \[F_j = \sum_{i<j}\frac{q_i q_j}{(i-j)^2 }-\sum_ ...

  2. Zyan 一个通信框架

    原文地址 本文示例 Zyan是一个简单直观的分布式应用程序开发框架. 以下是Zyan项目的简要概述. 架构图: Zyan一般由客户端和服务端组成.服务端(以组件的形式)提供服务,客户端远程调用服务端的 ...

  3. 诱惑当前 你的孩子能hold住吗?

    1.打好态度.动机.价值观基础 培养未成熟主体远大的志向,并不是向他们讲一些抽象的道理,而是根据他们的年龄特点,从形象的故事.童话开始,从身边的人物.事例出发,逐渐渗透一些人生的哲理.有一位自觉性非常 ...

  4. windows下用时间戳创建文件名

    英文环境下: echo Archive_%date:~-4,4%%date:~-10,2%%date:~-7,2%_%time:~0,2%%time:~3,2%%time:~6,2%.zip 中文: ...

  5. 【BZOJ】1702: [Usaco2007 Mar]Gold Balanced Lineup 平衡的队列

    [题意]给定n头牛,k个特色,给出每头牛拥有哪些特色的二进制对应数字,[i,j]平衡当且仅当第i~j头牛的所有特色数量都相等,求最长区间长度. [算法]平衡树+数学转化 [题解]统计前缀和sum[i] ...

  6. [Unity]扩展Hierachry的右键菜单

    游戏制作到一定阶段后,一定会出现一些GameObject的"模板".比如一个敌人一定会有一个"Enemy Behaviour"."Box Collid ...

  7. 【洛谷 P2042】 [NOI2005]维护数列(自闭记第一期)

    题目链接 首先,这题我是没A的..太毒瘤了 题目本身不难,都是\(Splay\)的基操,但是细节真的容易挂. 调了好久自闭了,果断放弃.. 希望本节目停更. 放上最终版本 #include <c ...

  8. https 多路复用的理解~转载

    https://segmentfault.com/q/1010000005167289 这里面的http/2连接是指tcp/ip层的连接还是http应用层(也就是我们平常在chrome F12 net ...

  9. STM32-内存管理

    转载:http://www.cnblogs.com/guozhikai/p/6031904.html #ifndef __MALLOC_H #define __MALLOC_H #include &q ...

  10. 给windows设置隐藏文件夹的方法

    cls @ECHO OFF title Folder Private if EXIST "HTG Locker" goto UNLOCK if NOT EXIST Private ...