首页 > BZOJ 2780: [Spoj]8093 Sevenk Love Oimaster( 后缀数组 + 二分 + RMQ + 树状数组 )

BZOJ 2780: [Spoj]8093 Sevenk Love Oimaster( 后缀数组 + 二分 + RMQ + 树状数组 )

全部串起来做SA, 在按字典序排序的后缀中, 包含每个询问串必定是1段连续的区间, 对每个询问串s二分+RMQ求出包含s的区间. 然后就是求区间的不同的数的个数(经典问题), sort queries + BIT 就行了.时间复杂度O(N log N). 速度垫底了QAQ 你们都会SAM。。。。

----------------------------------------------------------------------

#include
#include
#include
#include
#include
using namespace std;
 

#define b(i) (1 << (i))

const int maxL = 540009;
const int maxQ = 60009;
char S[maxL], str[maxL];
int N, n, q, Id[maxL], qL[maxQ], qR[maxQ], L[maxQ], R[maxQ];
int Rank[maxL], Height[maxL], Sa[maxL], cnt[maxL];
int RMQ[20][maxL], r[maxQ], ans[maxQ];
void Build() {
int m = 'z' + 1, *x = Rank, *y = Height;
for(int i = 0; i < m; i++) cnt[i] = 0;
for(int i = 0; i < N; i++) cnt[x[i] = S[i]]++;
for(int i = 1; i < m; i++) cnt[i] += cnt[i - 1];
for(int i = N; i--; ) Sa[--cnt[x[i]]] = i;
for(int k = 1, p = 0; k <= N; k <<= 1, p = 0) {
for(int i = N - k; i < N; i++) y[p++] = i;
for(int i = 0; i < N; i++)
if(Sa[i] >= k) y[p++] = Sa[i] - k;
for(int i = 0; i < m; i++) cnt[i] = 0;
for(int i = 0; i < N; i++) cnt[x[y[i]]]++;
for(int i = 1; i < m; i++) cnt[i] += cnt[i - 1];
for(int i = N; i--; ) Sa[--cnt[x[y[i]]]] = y[i];
swap(x, y);
x[Sa[0]] = 0;
p = 1;
for(int i = 1; i < N; i++) {
if(y[Sa[i]] != y[Sa[i - 1]] || y[Sa[i] + k] != y[Sa[i - 1] + k]) p++;
x[Sa[i]] = p - 1;
}
if((m = p) >= N) break;
}
for(int i = 0; i < N; i++) Rank[Sa[i]] = i;
Height[0] = Height[N] = 0;
for(int i = 0, h = 0; i < N; i++) if(Rank[i]) {
if(h) h--;
while(S[i + h] == S[Sa[Rank[i] - 1] + h]) h++;
Height[Rank[i]] = h;
}
}
void Init_RMQ() {
for(int i = 0; i < N; i++)
RMQ[0][i] = Height[i];
for(int i = 1; b(i) <= N; i++)
for(int j = 0; j + b(i) <= N; j++)
RMQ[i][j] = min(RMQ[i - 1][j], RMQ[i - 1][j + b(i - 1)]);
}
inline int LCP(int l, int r) {
int t = log2(r - l + 1);
return min(RMQ[t][l], RMQ[t][r - b(t) + 1]);
}
void calc(int &L, int &R, int p, int len) {
int _l, _r;
p = Rank[p];
if(Height[p] >= len) {
_l = 0, _r = p - 1;
while(_l <= _r) {
int m = (_l + _r) >> 1;
if(LCP(m + 1, p) >= len)
L = m, _r = m - 1;
else
_l = m + 1;
}
} else
L = p;
if(Height[p + 1] >= len) {
_l = p + 1, _r = N - 1;
while(_l <= _r) {
int m = (_l + _r) >> 1;
if(LCP(p + 1, m) >= len)
R = m, _l = m + 1;
else
_r = m - 1;
}
} else
R = p;
}
struct Link {
int p;
Link* n;
} pool[maxL], *pt = pool, *H[maxL];
inline void AddL(int v, int p) {
pt->p = p, pt->n = H[v], H[v] = pt++;
}
int B[maxL];
inline void Modify(int p, int v) {
if(!p) return;
for(; p <= N; p += p & -p) B[p] += v;
}
inline int Sum(int p) {
int ret = 0;
for(; p; p -= p & -p) ret += B[p];
return ret;
}
inline bool Cmp(const int &l, const int &r) {
return qL[l] < qL[r];
}
void Work() {
Build();
Init_RMQ();
memset(B, 0, sizeof B);
for(int i = N; i--; )
if(Id[Sa[i]] >= 0) AddL(Id[Sa[i]], i);
for(int i = 0; i < n; i++)
Modify(H[i]->p + 1, 1);
for(int i = 0; i < q; i++)
calc(qL[r[i] = i], qR[i], L[i], R[i] - L[i]);
sort(r, r + q, Cmp);
int c = 0;
for(int i = 0; i < N; i++) {
while(qL[r[c]] == i) {
ans[r[c]] = Sum(qR[r[c]] + 1) - Sum(qL[r[c]]);
if(++c >= q) break;
}
if(c >= q) break;
Modify(i + 1, -1);
if(H[Id[Sa[i]]]) {
H[Id[Sa[i]]] = H[Id[Sa[i]]]->n;
if(H[Id[Sa[i]]])
Modify(H[Id[Sa[i]]]->p + 1, 1);
}
}
for(int i = 0; i < q; i++)
printf("%d ", ans[i]);
}
inline int getstr() {
char c = getchar();
for(; !islower(c); c = getchar());
int len = 0;
for(; islower(c); c = getchar())
str[len++] = c;
return len;
}
void Init() {
scanf("%d%d", &n, &q);
N = 0;
int len;
for(int i = 0; i < n; i++) {
len = getstr();
for(int j = 0; j < len; j++) {
Id[N] = i;
S[N++] = str[j];
}
Id[N] = -1;
S[N++] = '$';
}
for(int i = 0; i < q; i++) {
len = getstr();
L[i] = N;
for(int j = 0; j < len; j++) {
Id[N] = -1;
S[N++] = str[j];
}
R[i] = N;
Id[N] = -1;
S[N++] = '$';
}
S[N - 1] = 0;
}
int main() {
Init();
Work();
return 0;
}

----------------------------------------------------------------------

2780: [Spoj]8093 Sevenk Love Oimaster

Time Limit: 1 Sec  Memory Limit: 128 MB

Submit: 581  Solved: 188

[Submit][Status][Discuss]

Description

     Oimaster and sevenk love each other.

    But recently,sevenk heard that a girl named ChuYuXun was dating with oimaster.As a woman's nature, sevenk felt angry and began to check oimaster's online talk with ChuYuXun.    Oimaster talked with ChuYuXun n times, and each online talk actually is a string.Sevenk asks q questions like this,    "how many strings in oimaster's online talk contain this string as their substrings?"


Input



There are two integers in the first line, 
the number of strings n and the number of questions q.
And n lines follow, each of them is a string describing oimaster's online talk. 
And q lines follow, each of them is a question.
n<=10000, q<=60000 
the total length of n strings<=100000, 
the total length of q question strings<=360000


Output

For each question, output the answer in one line.

Sample Input

3 3

abcabcabc

aaa

aafe

abc

a

ca

Sample Output

1

3

1

HINT

Source

 

转载于:https://www.cnblogs.com/JSZX11556/p/5188453.html

更多相关:

  •         Apache POI是一个开源的利用Java读写Excel,WORD等微软OLE2组件文档的项目。        我的需求是对Excel的数据进行导入或将数据以Excel的形式导出。先上简单的测试代码:package com.xing.studyTest.poi;import java.io.FileInputSt...

  • 要取得[a,b)的随机整数,使用(rand() % (b-a))+ a; 要取得[a,b]的随机整数,使用(rand() % (b-a+1))+ a; 要取得(a,b]的随机整数,使用(rand() % (b-a))+ a + 1; 通用公式:a + rand() % n;其中的a是起始值,n是整数的范围。 要取得a到b之间的...

  • 利用本征图像分解(Intrinsic Image Decomposition)算法,将图像分解为shading(illumination) image 和 reflectance(albedo) image,计算图像的reflectance image。 Reflectance Image 是指在变化的光照条件下能够维持不变的图像部分...

  • 题目:面试题39. 数组中出现次数超过一半的数字 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字。 你可以假设数组是非空的,并且给定的数组总是存在多数元素。 示例 1: 输入: [1, 2, 3, 2, 2, 2, 5, 4, 2] 输出: 2 限制: 1 <= 数组长度 <= 50000 解题: cl...

  • 题目:二叉搜索树的后序遍历序列 输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历结果。如果是则返回 true,否则返回 false。假设输入的数组的任意两个数字都互不相同。 参考以下这颗二叉搜索树:      5     /    2   6   /  1   3示例 1: 输入: [1,6,3,2,5] 输出...

  • 用python编写乘法口诀表的方法 发布时间:2020-08-25 11:46:35 来源:亿速云 阅读:60 作者:小新 用python编写乘法口诀表的方法?这个问题可能是我们日常学习或工作经常见到的。希望通过这个问题能让你收获颇深。下面是小编给大家带来的参考内容,让我们一起来看看吧! 第一种:使用for遍历循环嵌套for x in...

  • //很长一段时间我都只使用以下方式做数组循环,具体原因看数据 var aa = for (var i = 0, l = aa.length; i < l; i++) { var a = aa[i];} 数据采集图片来源于网友 很明显,for循环第二种方式完胜!!! 至于for in、forEach什么的,不知道甩他们多少...

  • 目录 1. Scene Graph Generation with External Knowledge and Image Reconstruction 2. Knowledge Acquisition for Visual Question Answering via Iterative Querying Author...

  • 基础题1: 输入一个正整数 n (1≤n≤10)和n 阶方阵a的元素,如果方阵a中的所有元素都沿主对角线对称,输出“Yes”, 否则,输出“No”。主对角线为从矩阵的左上角至右下角的连线,方阵a中的所有元素都沿主对角线对称指对所有i, k,a[i][k]和a[k][i]相等。输入输出示例如下: 输入: 3 1 2 3 4 5 6 7...

  • 程序流程控制 分支 顺序 循环 if switch&case 1 2 3 调整 break 1.6 前 switch(byte、short、char、int) 1.7 可放String 循环 while(次数不确定) do while for(确定次数) break(跳出本层循环) continue(跳出本次循环)     *   2...