POJ 1200:Crazy Search(哈希)
Crazy Search
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 32483 | Accepted: 8947 |
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
题意
给出一个字符串,将字符串分成长度为n的子串(连续的)。问这些子串中有多少不同的子串
思路
记录每个子串的哈希值,存在数组里,去重后输出数组的长度即可
AC代码
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <limits.h>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <string>
#define ll long long
#define ull unsigned long long
#define ms(a) memset(a,0,sizeof(a))
#define pi acos(-1.0)
#define INF 0x7f7f7f7f
const double E=exp(1);
const int maxn=1e6+10;
const int mod=1e9+7;
const int base=1007;
using namespace std;
ull a[maxn];
ull b[maxn];
char ch[maxn];
char c[maxn];
// 计算哈希值
ull get_hash(char ch[],int k,int l)
{
ull ans=0;
for(int i=k;i<=k+l-1;i++)
{
ans=(ans*base+ch[i])%mod;
}
return ans;
}
int main(int argc, char const *argv[])
{
ios::sync_with_stdio(false);
int n,m;
cin>>n>>m;
cin>>ch;
int l=strlen(ch);
for(int i=1;i<=l;i++)
c[i]=ch[i-1];
int k=0;
for(int i=1;i<=l-n+1;i++)
{
b[k++]=get_hash(c,i,n);
}
sort(b,b+k);
// 去重
k=unique(b,b+k)-b;
cout<<k<<endl;
return 0;
}
POJ 1200:Crazy Search(哈希)的更多相关文章
- 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(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
http://poj.org/problem?id=1200 #include<iostream> #include<cstring> using namespace std; ...
- POJ 1200 Crazy Search 字符串的Hash查找
第一次涉及HASH查找的知识 对于字符串的查找有很多前人开发出来的HASH函数,比较常用的好像是ELF 和 BKDR. 这道题没想到突破点是在于其nc值,告诉你组成字符串的字母种类. 还有用26进制, ...
- POJ 1200 Crazy Search
思路:利用Karp-Rabin算法的思想,对每个子串进行Hash,如果Hash值相等则认为这两个子串是相同的(事实上还需要做进一步检查),Karp-Rabin算法的Hash函数有多种形式,但思想都是把 ...
- POJ 1200 Crazy Search 【hash】
<题目链接> 题目大意: 给定n,nc,和一个字符串,该字符串由nc种字符组成,现在要你寻找该字符串中长度为n的子字符串有多少种. 解题分析: 因为要判重,所以讲这些字符串hash一下,将 ...
- POJ 1200 Crazy Search【Hash入门】
RK法:https://www.cnblogs.com/16crow/p/6879988.html #include<cstdio> #include<string> #inc ...
- Crazy Search POJ - 1200 (字符串哈希hash)
Many people like to solve hard puzzles some of which may lead them to madness. One such puzzle could ...
- poj 1200 crasy search
https://vjudge.net/problem/POJ-1200 题意: 给出一个字符串,给出子串的长度n和给出的字符串中不同字符的个数nc,统计这个字符串一共有多少不同的长度为n的子串. 思路 ...
随机推荐
- git Please move or remove them before you can merge.
git clean -d -fx "" 其中 x -----删除忽略文件已经对git来说不识别的文件 d -----删除未被添加到git的路径中的文件 f -----强制运行
- laravel中db获取某个数据的具体字段值:
$helpfriend = DB::connection('luckyrecord')->table($luckyrecord)->where('id', $luckyrecordid)- ...
- useful links about machine learning
机器学习(Machine Learning)&深度学习(Deep Learning)资料(Chapter 1) 机器学习(Machine Learning)&深度学习(Deep Lea ...
- ไม่มีวันเช่น--会有那么一天--电影《初恋这件小事》插曲--IPA--泰语
小清新的泰国的<初恋这件小事>插曲.
- byte[]->new String(byte[]) -> getByte()引发的不一致问题
今天接短信接口,短信接口提供了sdk,我们可以直接用sdk发送请求然后发送对应短信. 但是想使用我们平台自定义的httpUtil实现. 然而忙了1天半,才解决这个问题,还是我同事帮忙找出问题并解决的. ...
- 安装gcc
yum -y install gcc yum -y install gcc-c++ yum install make -- 或者 yum groupinstall "Developmen ...
- nginx配置文服
修改nginx.conf 添加如下内容 autoindex on; # 显示目录 autoindex_exact_size on; # 显示文件大小 autoindex_localtime on; # ...
- SpringMVC学习四(@ModelMap @RequestBody等等的说明)
参考如下 http://www.cnblogs.com/HD/p/4107674.html http://www.cnblogs.com/qiankun-site/p/5774325.html @re ...
- Creating and Destroying Objects
Consider static factpry methods instead of construction 四个优点两个缺点 One advantage of static factory met ...
- Linux:Apache安装与启动
Apache安装与启动 1.查看是否安装:rpm -qa| grep httpd2.挂载系统 mount /dev/cdrom /mnt/cdrom3.进入软件包 cd /mnt/cdrom/Pack ...