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. spring事务的一些注意点

    参考文章 http://blog.csdn.net/qq_34021712/article/details/75949779   ©王赛超 1.在需要事务管理的地方加@Transactional 注解 ...

  2. python实现备份gitlab版本库并更改文件名

    脚本的功能是实现备份gitlab版本库,并修改备份后的文件名,成功后发送邮件至相关负责人,脚本如下: #!/usr/bin/env python # -*- coding:utf-8 -*- impo ...

  3. 【BZOJ】1635: [Usaco2007 Jan]Tallest Cow 最高的牛

    [题意]n头牛,其中最高h.给定r组关系a和b,要求满足h[b]>=h[a]且a.b之间都小于min(h[a],h[b]),求第i头牛可能的最高高度. [算法]差分 [题解]容易发现r组关系只能 ...

  4. Html5学习1(Html属性、Html CSS:)

    Html属性 1.Html要求使用小写属性. Html标题 1.确保将Html标题标签只用于标题.不要仅仅为了生成粗体或大号的文本而使用标题. 2.<hr>标签在Html页面中创建水平线, ...

  5. UIImageView与UIScrollView的关系图

        UIImageView与UIScrollView的关系图           https://www.evernote.com/shard/s227/sh/0af9f23c-08e6-4be6 ...

  6. Python 下调用C动态链接库 -- (转)

    在linux开发的动态链接库需要被python调用,首先需要生成.so文件. 生成动态链接库的方法网上有很多,主要就是首先根据源文件编译生成.o,然后链接这些.o文件-shared生成.so.需要注意 ...

  7. ORA-02291:parent key not found

    Hibernate operation: Could not execute JDBC batch update; SQL [insert into dchnpricecarchancesource ...

  8. Java回收方法区中回收的类

    回收的类必须满足下面三个条件才能算是“无用的类” 1.该类所有的实例都已经被回收,也就是说Java堆中不存在该类的任何实例: 2.加载该类的ClassLoader已经被回收: 3.该类对应的java. ...

  9. spin_USACO

    Spinning Wheels1998 ACM NE Regionals Each of five opaque spinning wheels has one or more wedges cut ...

  10. Vim中的键映射【转】

    转自:http://www.cnblogs.com/softwaretesting/archive/2011/09/28/2194515.html http://www.pythonclub.org/ ...