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. [技巧篇]07.JSON.parse() 和 JSON.stringify()

    JSON.parse() 用于从一个字符串中解析出json对象,如 var str = '{"name":"huangxiaojian","age&q ...

  2. redis初试Not all 16384 slots are covered by nodes

    按照这里的步骤玩redis集群,http://www.redis.cn/topics/cluster-tutorial.html ./src/redis-trib.rb create --replic ...

  3. UVA 1649 Binomial coefficients

    https://vjudge.net/problem/UVA-1649 题意: 输入m,求所有的C(n,k)=m m<=1e15 如果枚举n,那么C(n,k)先递增后递减 如果枚举k,那么C(n ...

  4. spoj COT2 - Count on a tree II

    COT2 - Count on a tree II http://www.spoj.com/problems/COT2/ #tree You are given a tree with N nodes ...

  5. Codeforces Round #380 (Div. 2)/729D Sea Battle 思维题

    Galya is playing one-dimensional Sea Battle on a 1 × n grid. In this game a ships are placed on the ...

  6. SourceTree for mac 注册过程(v2.7.6a)

    背景 为啥要自己注册呢,往上一堆一堆的老版本许可证偏不用,就愿意定制自己的账号style. 搞了半天,还是觉得pycharm自带的git工具就挺好用了,闲的没事记录一下. 要点 百度搜索的地址可以进入 ...

  7. 删除linux上7天前后缀名.sql的文件

    #!/bin/bash#delete the file of 7 days agofind /data/mysqlbackup/ -mtime +7 -name "*.sql" - ...

  8. NDK---使用,开发步骤

    使用NDk的场景: 1.某些方法,是使用C,C++本地代码实现的,然后,我想在Java中调用这些方法.这个时候,就需要使用到JNI技术. 应用NDK的时候,分两个部分,Java部分,JNI层部分,本地 ...

  9. [LA3523/uva10195]圆桌骑士 tarjan点双连通分量+奇环定理+二分图判定

    1.一个环上的各点必定在同一个点双连通分量内: 2.如果一个点双连通分量是二分图,就不可能有奇环: 最基本的二分图中的一个环: #include<cstdio> #include<c ...

  10. 多种方法过Codeforces Round #270的A题(奇偶法、打表法和Miller_Rabin(这个方法才是重点))

    题目链接:http://codeforces.com/contest/472/problem/A 题目: 题意:哥德巴赫猜想是:一个大于2的素数一定可以表示为两个素数的和.此题则是将其修改为:一个大于 ...