POJ 1200 Crazy Search【Hash入门】
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入门】的更多相关文章
- poj 1200 Crazy Search(hash)
题目链接:http://poj.org/problem?id=1200 思路分析:从数据来看,该题目使用线性时间算法,可见子串的比较是不可能的:使用hash可以在常数时间内查找,可以常数时间内判重, ...
- POJ 1200 Crazy Search(字符串简单的hash)
题目:http://poj.org/problem?id=1200 最近看了一个关于hash的问题,不是很明白,于是乎就找了些关于这方面的题目,这道题是一道简单的hash 字符串题目,就先从他入手吧. ...
- POJ 1200 Crazy Search 【hash】
<题目链接> 题目大意: 给定n,nc,和一个字符串,该字符串由nc种字符组成,现在要你寻找该字符串中长度为n的子字符串有多少种. 解题分析: 因为要判重,所以讲这些字符串hash一下,将 ...
- POJ 1200 Crazy Search 字符串的Hash查找
第一次涉及HASH查找的知识 对于字符串的查找有很多前人开发出来的HASH函数,比较常用的好像是ELF 和 BKDR. 这道题没想到突破点是在于其nc值,告诉你组成字符串的字母种类. 还有用26进制, ...
- POJ 1200 Crazy Search (哈希)
题目链接 Description Many people like to solve hard puzzles some of which may lead them to madness. One ...
- POJ – 1200 Crazy Search
http://poj.org/problem?id=1200 #include<iostream> #include<cstring> using namespace std; ...
- POJ 1200 Crazy Search
思路:利用Karp-Rabin算法的思想,对每个子串进行Hash,如果Hash值相等则认为这两个子串是相同的(事实上还需要做进一步检查),Karp-Rabin算法的Hash函数有多种形式,但思想都是把 ...
- [poj1200]Crazy Search(hash)
Crazy Search Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 26713 Accepted: 7449 Descrip ...
- poj1200-Crazy Search(hash入门经典)
Hash:一般是一个整数.就是说通过某种算法,可以把一个字符串"压缩" 成一个整数.一,题意: 给出两个数n,nc,并给出一个由nc种字符组成的字符串.求这个字符串中长度为n的不同 ...
随机推荐
- LightOJ 1062 - Crossed Ladders 基础计算几何
http://www.lightoj.com/volume_showproblem.php?problem=1062 题意:问两条平行边间的距离,给出从同一水平面出发的两条相交线段长,及它们交点到水平 ...
- 元类编程-- __new__和__init__的区别
class User: def __new__(cls, *args, **kwargs): print (" in new ") return super().__new__(c ...
- 【HDU】5269 ZYB loves Xor I
[算法]trie [题解] 为了让数据有序,求lowbit无法直接排序,从而考虑倒过来排序,然后数据就会呈现出明显的规律: 法一:将数字倒着贴在字典树上,则容易发现两数的lowbit就是它们岔道结点的 ...
- 多线程---iOS-Apple苹果官方文档翻译
本系列所有开发文档翻译链接地址:iOS7开发-Apple苹果iPhone开发Xcode官方文档翻译PDF下载地址(2013年12月29日更新版) 多线程 技术博客http://www.cnblo ...
- SQL Server Delete Duplicate Rows
There can be two types of duplication of rows in a table 1. Entire row getting duplicated because th ...
- 小程序_RSA加密功能
这是开发的第三个小程序,基于一个物流系统,简化功能开发下单流程.登录的时候,系统是使用RSA进行加解密的. 流程:第一个接口获取到后端传过来的密匙共钥(publicKey),通过公钥使用RSA加密密码 ...
- hdu 1217 Arbitrage (spfa算法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1217 题目大意:通过货币的转换,来判断是否获利,如果获利则输出Yes,否则输出No. 这里介绍一个ST ...
- SourceTree 过期,注册导入许可证
参考这里:SourceTree过期,需要注册导入 SourceTree License 许可证 很详细 补充: 如果在 SourceTree 软件里注册失败,可以在网页注册. 如果其他邮箱不支持,可以 ...
- pillow模块的学习
https://github.com/wangbinyq/pillow_example http://pillow.readthedocs.org/en/latest/handbook/tutoria ...
- Linux内核中内存cache的实现【转】
Linux内核中内存cache的实现 转自:http://blog.chinaunix.net/uid-127037-id-2919545.html 本文档的Copyleft归yfydz所有,使用 ...