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的子串. 思路 ...
随机推荐
- learning scala read from file
scala读文件: example: scala> import scala.io.Sourceimport scala.io.Source scala> var inputFile ...
- day06 元组类型
一.什么是元组? 元组就是一个不可变的列表 元组的基本使用: 1.用途: 用于存放多个值,当存放多个任意类型的值 2.定义方式:在()内用逗号分隔开多个任意类型的值 t=(1,3.1,'aaa',( ...
- 和的java程序
- python 数据如何保存到excel中--xlwt
第一步:下载xlwt 首先要下载xlwt,(前提是你已经安装好了Python) 下载地址: https://pypi.python.org/pypi/xlwt/ 下载第二个 第二步:安装xl ...
- maven配置checkstyle插件对代码规范进行静态检查
checkstyle配置的官方网站:http://checkstyle.sourceforge.net/config.html (1)新建maven项目,配置checkstyle插件 pom.xml ...
- flask-admin有用的例子
flask-admin主页: https://github.com/flask-admin/flask-admin flask-admin克隆地址: https://github.com/flask- ...
- java中的package
java中用于存放源文件的文件夹叫做包package package中可以有源文件也可以由其他包. package的“全限定名”不是从磁盘的根目录开始的,而是从源代码的根目录开始的,以点号“.”作为分 ...
- Python数据分析中对重复值、缺失值、空格的处理
对重复值的处理 把数据结构中,行相同的数据只保留一行 函数语法: drop_duplicates() from pandas import read_csv df = read_csv(文件位置) n ...
- linux 创建安装redis服务
1.找下redis的官方的下载地址:http://download.redis.io/releases/redis-3.2.8.tar.gz 有最新的就下载最新 先下载解压跟安装 wget http ...
- Xilinx SDK编译Microblaze时出错
reference:http://www.eeboard.com/evaluation/digilent-cmod-a7-fpga/9/ 在vivado 2015.4中创建microblaze软核,l ...