http://acm.hdu.edu.cn/showproblem.php?pid=5069

首先判断suffix和prefix最长多少可以直接暴力枚举长度然后 + hash可以立马判断是否相等,复杂度O(lenstr)

现在知道总长度 <= 1e5,  magic = sqrt(lenstr)

那么,

如果长度是 <= magic的,最多1e5个询问,每次询问我暴力也是才1e5 * sqrt(1e5)的复杂度。

如果长度是 >= magic的,

第一个极端情况是有magic个不同的串,这样就可以有magic^2个不同的询问,但是这样每个串的长度最长是magic,这样暴力也是才magic * magic * magic  = 1e5 * sqrt(1e5)的复杂度。

第二个极端情况是只有2个串,每个串长度都很长,1e5 / 2 的长度,这样暴力一次就需要O(1e5)的复杂度了。但是如果这样的话,不同的询问就会很少,可以用个ans数组存起来,ans[a][b],这样总复杂度也是 <= 1e5 * sqrt(1e5)的复杂度。

所以总复杂度是nsqrtn

#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL;
const int maxn = 1e5 + ;
char str[maxn];
int po[maxn], suffix[maxn], prefix[maxn];
int be[maxn], en[maxn];
int n, q;
int ans[][];
int getAns(int one, int two) {
for (int i = min(en[two] - be[two] + , en[one] - be[one] + ); i >= ; --i) {
if (prefix[be[two] + i - ] == suffix[en[one] - i + ]) {
return i;
}
}
return ;
}
int pos[maxn];
void work() {
scanf("%d%d", &n, &q);
int magic = (int)sqrt(n * 1.0);
int to = ;
for (int i = ; i <= n; ++i) {
scanf("%s", str + en[i - ] + );
int len = strlen(str + en[i - ] + );
be[i] = en[i - ] + , en[i] = en[i - ] + len;
prefix[be[i]] = str[be[i]];
for (int j = be[i] + ; j <= en[i]; ++j) {
prefix[j] = prefix[j - ] * + str[j];
}
suffix[en[i]] = str[en[i]];
for (int j = en[i] - ; j >= be[i]; --j) {
suffix[j] = str[j] * po[en[i] - j] + suffix[j + ];
}
if (len > magic) {
pos[i] = ++to;
}
}
// printf("%s\n", str + 1);
// for (int i = 1; i <= n; ++i) {
// printf("%d %d\n", be[i], en[i]);
// }
for (int i = ; i <= to; ++i) {
for (int j = ; j <= to; ++j) {
ans[i][j] = -;
}
}
while (q--) {
int one, two;
scanf("%d%d", &one, &two);
if (en[one] - be[one] + <= magic || en[two] - be[two] + <= magic) {
printf("%d\n", getAns(one, two));
} else {
if (ans[pos[one]][pos[two]] == -) {
ans[pos[one]][pos[two]] = getAns(one, two);
}
printf("%d\n", ans[pos[one]][pos[two]]);
}
}
}
int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
po[] = ;
for (int i = ; i <= maxn - ; ++i) po[i] = po[i - ] * ;
while (scanf("%d%d", &n, &q) > ) work();
return ;
}

https://www.nowcoder.com/acm/contest/94/E

#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef unsigned long long int LL;
const int maxn = 1e6 + ;
char str[maxn];
LL po[maxn], suffix[maxn], prefix[maxn];
int be[maxn], en[maxn];
int n, q;
int ans[][];
int getAns(int one, int two) {
for (int i = min(en[two] - be[two] + , en[one] - be[one] + ); i >= ; --i) {
if (prefix[be[two] + i - ] == suffix[en[one] - i + ]) {
return i;
}
}
return ;
}
int pos[maxn];
void work() {
int magic = (int)sqrt( * 1.0);
int to = ;
for (int i = ; i <= n; ++i) {
scanf("%s", str + en[i - ] + );
int len = strlen(str + en[i - ] + );
be[i] = en[i - ] + , en[i] = en[i - ] + len;
prefix[be[i]] = str[be[i]];
for (int j = be[i] + ; j <= en[i]; ++j) {
prefix[j] = prefix[j - ] * + str[j];
}
suffix[en[i]] = str[en[i]];
for (int j = en[i] - ; j >= be[i]; --j) {
suffix[j] = str[j] * po[en[i] - j] + suffix[j + ];
}
if (len > magic) {
pos[i] = ++to;
}
}
// printf("%s\n", str + 1);
// for (int i = 1; i <= n; ++i) {
// printf("%d %d\n", be[i], en[i]);
// }
for (int i = ; i <= to; ++i) {
for (int j = ; j <= to; ++j) {
ans[i][j] = -;
}
}
cin >> q;
while (q--) {
int one, two;
scanf("%d%d", &one, &two);
if (en[one] - be[one] + <= magic || en[two] - be[two] + <= magic) {
printf("%d\n", getAns(one, two));
} else {
if (ans[pos[one]][pos[two]] == -) {
ans[pos[one]][pos[two]] = getAns(one, two);
}
printf("%d\n", ans[pos[one]][pos[two]]);
}
}
}
int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
po[] = ;
for (int i = ; i <= maxn - ; ++i) po[i] = po[i - ] * ;
while (scanf("%d", &n) > ) work();
return ;
}

Harry And Biological Teacher 分块 + 字符串hash的更多相关文章

  1. HDU 5763 Another Meaning dp+字符串hash || DP+KMP

    题意:给定一个句子str,和一个单词sub,这个单词sub可以翻译成两种不同的意思,问这个句子一共能翻译成多少种不能的意思 例如:str:hehehe   sub:hehe 那么,有**he.he** ...

  2. [知识点]字符串Hash

    1.前言 字符串的几大主要算法都多少提及过,现在来讲讲一个称不上什么算法, 但是非常常用的东西——字符串Hash. 2.Hash的概念 Hash更详细的概念不多说了,它的作用在于能够对复杂的状态进行简 ...

  3. 【BZOJ-3555】企鹅QQ 字符串Hash

    3555: [Ctsc2014]企鹅QQ Time Limit: 20 Sec  Memory Limit: 256 MBSubmit: 1545  Solved: 593[Submit][Statu ...

  4. POJ 1200 字符串HASH

    题目链接:http://poj.org/problem?id=1200 题意:给定一个字符串,字符串只有NC个不同的字符,问这个字符串所有长度为N的子串有多少个不相同. 思路:字符串HASH,因为只有 ...

  5. LA4671 K-neighbor substrings(FFT + 字符串Hash)

    题目 Source http://acm.hust.edu.cn/vjudge/problem/19225 Description The Hamming distance between two s ...

  6. 各种字符串Hash函数比较(转)

    常用的字符串Hash函数还有ELFHash,APHash等等,都是十分简单有效的方法.这些函数使用位运算使得每一个字符都对最后的函数值产生影响.另外还有以MD5和SHA1为代表的杂凑函数,这些函数几乎 ...

  7. 字符串hash + 二分答案 - 求最长公共子串 --- poj 2774

    Long Long Message Problem's Link:http://poj.org/problem?id=2774 Mean: 求两个字符串的最长公共子串的长度. analyse: 前面在 ...

  8. 字符串hash - POJ 3461 Oulipo

    Oulipo Problem's Link ---------------------------------------------------------------------------- M ...

  9. 长度有限制的字符串hash函数

    长度有限制的字符串hash函数 DJBHash是一种非常流行的算法,俗称"Times33"算法.Times33的算法很简单,就是不断的乘33,原型如下 hash(i) = hash ...

随机推荐

  1. 洛谷 1351 联合权值——树形dp

    题目:https://www.luogu.org/problemnew/show/P1351 对拍了一下,才发现自己漏掉了那种拐弯的情况. #include<iostream> #incl ...

  2. virtual judge(专题一 简单搜索 A)

    问题描述: Description 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘, ...

  3. linux日常管理-vmstat命令

    系统负载用w查看.是什么原因造成了系统负载.查看系统负载状态 命令:vmstat vmstat就查看一次 vmstat 1 每秒钟更新一次.按ctrl+c取消. vmstat 1 5 每秒钟更新一次, ...

  4. winform 客户端 HTTP协议与服务端通信以及解决中文乱码

    本来从来没有仔细研究过Http协议,今天因为公司业务需求,调试了半天,终于现在会Winform用Http协议与服务端通信了,其中常用的有POST和Get方式: 仔细看了人人网和新浪等大部分都是采用GE ...

  5. python fabric的安装与使用

    背景:fabric主要执行远程的shell命令,包括文件的上传.下载,以及提示用户输入等辅助其它功能. 测试系统:ubuntu16 要求:python //系统有自带,ubuntu16 通常自带pyt ...

  6. 对vuex的理解

    我用的vue安装了一个插件vuex插件 有3个 文件夹分别是actions(用于数据请求),getters(用于监听store),store(用于存储数据),

  7. 8、SRR数据下载https://ftp-trace.ncbi.nlm.nih.gov/sra/sdk/2.8.2/

    1.prefetch SRRxxxxxx         -/ncbi/public/sra 2.fastq-dump --split-files xxxxxxsra 3.SRA.SAM以及Fastq ...

  8. Vue 学习文档

    Vue 学习文档 vue 起步 引包 启动 new Vue(options) options: el 目的地(可以用类名.标签名等,也可以直接用mod元素) #elementId .elementCl ...

  9. 33、生鲜电商平台-定时器,定时任务quartz的设计与架构

    说明:任何业务有时候需要系统在某个定点的时刻执行某些任务,比如:凌晨2点统计昨天的报表,早上6点抽取用户下单的佣金. 对于Java开源生鲜电商平台而言,有定时推送客户备货,定时计算卖家今日的收益,定时 ...

  10. 【转】List<T>和ILIst<T>的区别

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace List ...