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

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

#include<cmath>
#include<cstdio>
#include<cctype>
#include<cstring>
#include<algorithm>
 
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\n", 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

BZOJ 2780: [Spoj]8093 Sevenk Love Oimaster( 后缀数组 + 二分 + RMQ + 树状数组 )的更多相关文章

  1. 三种做法:BZOJ 2780: [Spoj]8093 Sevenk Love Oimaster

    目录 题意 思路 AC_Code1 AC_Code2 AC_Code3 参考 @(bzoj 2780: [Spoj]8093 Sevenk Love Oimaster) 题意 链接:here 有\(n ...

  2. BZOJ 2780: [Spoj]8093 Sevenk Love Oimaster [广义后缀自动机]

    JZPGYZ - Sevenk Love Oimaster     Oimaster and sevenk love each other.       But recently,sevenk hea ...

  3. bzoj 2780 [Spoj]8093 Sevenk Love Oimaster

    LINK:Sevenk Love Oimaster 询问一个模式串在多少个文本串中出现过. 考虑广义SAM 统计这种数量问题一般有三种做法. 一种 暴力bitset 这道题可能可以过? 一种 暴力跳p ...

  4. bzoj 3277 串 && bzoj 3473 字符串 && bzoj 2780 [Spoj]8093 Sevenk Love Oimaster——广义后缀自动机

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3277 https://www.lydsy.com/JudgeOnline/problem.p ...

  5. BZOJ 2780 [Spoj]8093 Sevenk Love Oimaster ——广义后缀自动机

    给定n个串m个询问,问每个串在n个串多少个串中出现了. 构建广义后缀自动机,(就是把所有字符串的后缀自动机合并起来)其实只需要add的时候注意一下就可以了. 然后对于每一个串,跑一边匹配,到达了now ...

  6. 【刷题】BZOJ 2780 [Spoj]8093 Sevenk Love Oimaster

    Description Oimaster and sevenk love each other. But recently,sevenk heard that a girl named ChuYuXu ...

  7. bzoj 2780: [Spoj]8093 Sevenk Love Oimaster(广义SAM)

    题目大意:给出n个原串,再给出m个查询串.求每个查询串出现在了多少原串中. 题解 直接对原串建一个广义SAM,然后把每一个原串放到SAM上跑一跑,记录一下每一个状态属于多少个原串,用$size$表示. ...

  8. bzoj 2780: [Spoj]8093 Sevenk Love Oimaster【广义SAM】

    AC自动机比较简单,把询问串做成AC自动机然后模板串边跑变更新即可 SAM是把模板串做成广义SAM,然后每个节点存有几个模板串经过,具体方法是每次更新暴力向上跳直到有时间戳我不会证为什么时间复杂度是对 ...

  9. 【BZOJ2780】[Spoj]8093 Sevenk Love Oimaster 广义后缀自动机

    [BZOJ2780][Spoj]8093 Sevenk Love Oimaster Description Oimaster and sevenk love each other.     But r ...

随机推荐

  1. 《JavaScript 闯关记》之语法

    JavaScript 的语法大量借鉴了 C 及其他类 C 语言(如 Java 和 Perl)的语法.因此,熟悉这些语言的开发人员在接受 JavaScript 更加宽松的语法时,一定会有种轻松自在的感觉 ...

  2. ExtJs4.0入门错误

    当在eclipse中的web工程中增加了extjs4,出现An internal error occurred during: "Building workspace". Java ...

  3. ASP.NET中分布式事务的使用

    之前发表了一篇事务的存储过程,最近在做项目的时候遇到分布式事务,所有总结一下,跟大家分享和交流一下经验.首先说明为什么要分布式事务呢?先说说我在项目的哪里遇到分布式事务吧,我是在做网站后台开发的时候, ...

  4. Java的动态代理机制详解(转)

    在学习Spring的时候,我们知道Spring主要有两大思想,一个是IoC,另一个就是AOP,对于IoC,依赖注入就不用多说了,而对于Spring的核心AOP来说,我们不但要知道怎么通过AOP来满足的 ...

  5. html 调用 activeX(c++)

    1.新建MFC ActiveX 2.添加方法 3.找到add函数编写代码 4.在test.idl中找到最后一个uuid 5.编译工程,会自动注册控件 6.html中的代码 <html> & ...

  6. Js中JSON.stringify()与JSON.parse()与eval()详解及使用案例

    JSON(JavaScript Object Notation)是一种轻量级的数据交换格式.因为采用独立于语言的文本格式,也使用了类似于C语言家族的习惯,拥有了这些特性使使JSON称为理想的数据交换语 ...

  7. Caused by: android.view.InflateException: Binary XML file line #12: Error inflating class android.support.design.widget.TabLayout,TableLayout引起页面崩溃

    在使用TableLayout的时候,运行引用程序直接Crash. FATAL EXCEPTION: main Process: com.edaixi.activity, PID: 9703 java. ...

  8. 深刻理解Oracle数据库的启动和关闭 .

    Oracle数据库提供了几种不同的数据库启动和关闭方式,本文将详细介绍这些启动和关闭方式之间的区别以及它们各自不同的功能. 一.启动和关闭Oracle数据库 对于大多数Oracle DBA来说,启动和 ...

  9. How Many Tables--hdu1213(并查集)

    How Many Tables Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  10. Financial Management--hdu1064

    Financial Management Problem Description Larry graduated this year and finally has a job. He’s makin ...