POJ-1200-Crazy Search(字符串Hash)
Crazy Search
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 33142 Accepted: 9079
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
分析
- 串的长度最大为16000000,所以如果要用字符串Hash(把字符串当作一个数字)就要尽量使这个数字小。
- 利用字符在串中的出现顺序当作该字符的权值,而整个字符串就是nc进制数,然后把数字用hash表储存。
const int p = 131;
char has[16000001];
int a[128];
string str;
int n,nc;
int num = 0,cnt = 0;
int main()
{
cin>>n>>nc;
cin>>str;
int len = str.length();
for(int i=0;i<len;i++)
{
if(!a[str[i]])
a[str[i]] = ++num;
}
for(int i=0;i<len-n+1;i++)
{
int sum=0;
for(int j=i;j<i+n;j++)
sum = sum*nc+a[str[i]];
if(!has[sum])
has[sum] = 1,cnt++;
}
cout<<cnt<<endl;
}
POJ-1200-Crazy Search(字符串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 ...
- POJ 1200 Crazy Search(字符串简单的hash)
题目:http://poj.org/problem?id=1200 最近看了一个关于hash的问题,不是很明白,于是乎就找了些关于这方面的题目,这道题是一道简单的hash 字符串题目,就先从他入手吧. ...
- POJ 1200 Crazy Search 字符串的Hash查找
第一次涉及HASH查找的知识 对于字符串的查找有很多前人开发出来的HASH函数,比较常用的好像是ELF 和 BKDR. 这道题没想到突破点是在于其nc值,告诉你组成字符串的字母种类. 还有用26进制, ...
- poj 1200 Crazy Search(hash)
题目链接:http://poj.org/problem?id=1200 思路分析:从数据来看,该题目使用线性时间算法,可见子串的比较是不可能的:使用hash可以在常数时间内查找,可以常数时间内判重, ...
- 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函数有多种形式,但思想都是把 ...
- POJ 3461 Oulipo(字符串hash)
题目链接 字符串hash判断字符串是否相等. code #include<cstdio> #include<algorithm> #include<cstring> ...
- poj 1200 crasy search
https://vjudge.net/problem/POJ-1200 题意: 给出一个字符串,给出子串的长度n和给出的字符串中不同字符的个数nc,统计这个字符串一共有多少不同的长度为n的子串. 思路 ...
随机推荐
- css margin边界叠加问题详谈
问题:给子元素一个margin-top值,其父元素会跟着往下走margin-top的值 一.代码展示 HTML <body> <div class="box"&g ...
- 51单片机 小车 L298N pwm调速 串口控制 按键控制
难点:1.串口定时器T1,和T0定时器优先级 2.pwm频率与占空比的设置 按键控制 按键1——前进 按键2——后退 按键3——加速 按键4——减速 (板子上只有四个按键) 串口控制 ‘1’——前进 ...
- 用纯XMLHttpRequest实现AJAX
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- Luogu P3393 逃离僵尸岛【最短路】By cellur925
题目传送门 题目大意:(其实概括出来也就基本做完了hh)在一张有$n$个点,$m$条边的无向图上,有$k$个点是不能经过的,而与之距离不超过$s$的点,到他们会花费$Q$元,到其他点会花费$p$元,求 ...
- P2024 [NOI2001]食物链
第一种说法是"1 X Y",表示 X 和 Y 是同类. 第二种说法是"2 X Y",表示 X 吃 Y . 根据这句话 可以看出来这是一个种类并查集 种类并查集 ...
- 【loj10061】最短母串
#10061. 「一本通 2.4 练习 4」最短母串 内存限制:512 MiB 时间限制:1000 ms 标准输入输出 题目类型:传统 评测方式:文本比较 上传者: 1bentong 提交 提交 ...
- BFS Codeforces Round #297 (Div. 2) D. Arthur and Walls
题目传送门 /* 题意:问最少替换'*'为'.',使得'.'连通的都是矩形 BFS:搜索想法很奇妙,先把'.'的入队,然后对于每个'.'八个方向寻找 在2*2的方格里,若只有一个是'*',那么它一定要 ...
- EJB Timer Service is not available. Timers for application with id 95795415990861824 will not be deleted
delete follows:glassfish\domains\domain1\applications\ejb-timer-service-appglassfish\domains\domain1 ...
- Varnish快速安装及测试
实验环境: slave-147: 192.168.75.147 slave-148: 192.168.75.148 两台机器均已关闭selinux,关闭iptables. varnish部署 ...
- 《Head First HTML与CSS》的HTML标签、属性
一个标准的html5页面: <!doctype html> <html lang="zh-cmn-Hans"> <head> <meta ...