E. Forensic Examination

time limit per test:6 seconds
memory limit per test:768 megabytes
input:standard input
output:standard output

The country of Reberland is the archenemy of Berland. Recently the authorities of Berland arrested a Reberlandian spy who tried to bring the leaflets intended for agitational propaganda to Berland illegally . The most leaflets contain substrings of the Absolutely Inadmissible Swearword and maybe even the whole word.

Berland legal system uses the difficult algorithm in order to determine the guilt of the spy. The main part of this algorithm is the following procedure.

All the m leaflets that are brought by the spy are numbered from 1 to m. After that it's needed to get the answer to q queries of the following kind: "In which leaflet in the segment of numbers [l, r] the substring of the Absolutely Inadmissible Swearword [pl, pr] occurs more often?".

The expert wants you to automate that procedure because this time texts of leaflets are too long. Help him!

Input

The first line contains the string s (1 ≤ |s| ≤ 5·105) — the Absolutely Inadmissible Swearword. The string s consists of only lowercase English letters.

The second line contains the only integer m (1 ≤ m ≤ 5·104) — the number of texts of leaflets for expertise.

Each of the next m lines contains the only string ti — the text of the i-th leaflet. The sum of lengths of all leaflet texts doesn't exceed 5·104. The text of the leaflets consists of only lowercase English letters.

The next line contains integer q (1 ≤ q ≤ 5·105) — the number of queries for expertise.

Finally, each of the last q lines contains four integers lrplpr (1 ≤ l ≤ r ≤ m, 1 ≤ pl ≤ pr ≤ |s|), where |s| is the length of the Absolutely Inadmissible Swearword.

Output

Print q lines. The i-th of them should contain two integers — the number of the text with the most occurences and the number of occurences of the substring [pl, pr] of the string s. If there are several text numbers print the smallest one.

Examples
input
suffixtree
3
suffixtreesareawesome
cartesiantreeisworsethansegmenttree
nyeeheeheee
2
1 2 1 10
1 3 9 10
output
1 1
3 4

Solution

题目大意:给出一个模板串S和M个特殊串,每次询问S的[l,r]这个子串出现在编号为[pl,pr]的特殊串中最多出现次数以及其编号。

显然可以把所有串连起来建后缀自动机。

对于每个特殊串的节点,可以认为它包含一种颜色,然后查询操作实际上就是查询一个子树颜色数。

这个可以从叶子节点利用线段树合并得到;

总的复杂度是$O(NlogN)$

Code

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
using namespace std;
inline int read()
{
int x=0,f=1; char ch=getchar();
while (ch<'0' || ch>'9') {if (ch=='-') f=-1; ch=getchar();}
while (ch>='0' && ch<='9') {x=x*10+ch-'0'; ch=getchar();}
return x*f;
} #define MAXN 500010
#define MAXS 1230010 int N,M,Q,pos[MAXN]; char S[MAXN],s[MAXN]; int root=1,sz=1,last=1,par[MAXS],son[MAXS][27],len[MAXS],st[MAXS],id[MAXS];
inline void Extend(int c)
{
int cur=++sz,p=last;
len[cur]=len[p]+1;
while (p && !son[p][c]) son[p][c]=cur,p=par[p];
if (!p) par[cur]=root;
else {
int q=son[p][c];
if (len[p]+1==len[q]) par[cur]=q;
else {
int nq=++sz;
memcpy(son[nq],son[q],sizeof(son[nq]));
len[nq]=len[p]+1; par[nq]=par[q];
while (p && son[p][c]==q) son[p][c]=nq,p=par[p];
par[q]=par[cur]=nq;
}
}
last=cur;
} inline void Sort()
{
for (int i=0; i<=sz; i++) st[i]=0;
for (int i=1; i<=sz; i++) st[len[i]]++;
for (int i=0; i<=sz; i++) st[i]+=st[i-1];
for (int i=1; i<=sz; i++) id[st[len[i]]--]=i;
} #define Pa pair<int,int>
#define MP make_pair
#define Max first
#define Id second struct SgtNode{
Pa key;
int lson,rson;
}tree[MAXS*23]; inline Pa operator * (const Pa & A,const Pa &B) {return A.Max==B.Max? (A.Id<B.Id? A:B):(A.Max>B.Max? A:B);} inline Pa operator + (const Pa & A,const Pa &B) {return MP(A.Max+B.Max,A.Id);} int cnt,roots[MAXS],father[21][MAXS];
inline void Insert(int &x,int val,int l,int r)
{
if(!x) x=++cnt;
if (l==r) {tree[x].key.Max++,tree[x].key.Id=l; return;}
int mid=(l+r)>>1;
if (val<=mid) Insert(tree[x].lson,val,l,mid);
else Insert(tree[x].rson,val,mid+1,r);
tree[x].key=tree[tree[x].lson].key * tree[tree[x].rson].key;
} inline int Merge(int x,int y,int l,int r)
{
if (!x || !y) return x|y;
int z=++cnt;
if (l==r) {
tree[z].key=tree[x].key+tree[y].key;
return z;
}
int mid=(l+r)>>1;
tree[z].lson=Merge(tree[x].lson,tree[y].lson,l,mid);
tree[z].rson=Merge(tree[x].rson,tree[y].rson,mid+1,r);
tree[z].key=tree[tree[z].lson].key * tree[tree[z].rson].key;
return z;
} inline Pa Query(int x,int l,int r,int L,int R)
{
if (!x) return MP(0,0);
if (L<=l && R>=r) return tree[x].key;
int mid=(l+r)>>1;
if (R<=mid) return Query(tree[x].lson,l,mid,L,R);
else if (L>mid) return Query(tree[x].rson,mid+1,r,L,R);
else return Query(tree[x].lson,l,mid,L,mid) * Query(tree[x].rson,mid+1,r,mid+1,R);
} inline int Get(int l,int r)
{
int Len=r-l+1,x=pos[r];
for (int i=20; i>=0; i--)
if (len[father[i][x]]>=Len)
x=father[i][x];
return x;
} int main()
{
scanf("%s",S+1); N=strlen(S+1);
for (int i=1; i<=N; i++) Extend(S[i]-'a'),pos[i]=last;
Extend(26);
M=read();
for (int j=1; j<=M; j++) {
scanf("%s",s+1); int le=strlen(s+1);
for (int i=1; i<=le; i++) {
Extend(s[i]-'a'),Insert(roots[last],j,1,M);
}
Extend(26);
} Sort(); for (int i=sz; i>=1; i--) {
int x=id[i];
if (par[x]) roots[par[x]]=Merge(roots[par[x]],roots[x],1,M);
} for (int i=1; i<=sz; i++) father[0][i]=par[i]; for (int j=1; j<=20; j++)
for (int i=1; i<=sz; i++)
father[j][i]=father[j-1][father[j-1][i]]; Q=read();
while (Q--) {
int l=read(),r=read(),pl=read(),pr=read();
int x=Get(pl,pr);
Pa ans=Query(roots[x],1,M,l,r);
if (!ans.Max) printf("%d %d\n",l,0);
else printf("%d %d\n",ans.Id,ans.Max);
} return 0;
}

  

【Codeforces666E】Forensic Examination 后缀自动机 + 线段树合并的更多相关文章

  1. cf666E. Forensic Examination(广义后缀自动机 线段树合并)

    题意 题目链接 Sol 神仙题Orz 后缀自动机 + 线段树合并 首先对所有的\(t_i\)建个广义后缀自动机,这样可以得到所有子串信息. 考虑把询问离线,然后把\(S\)拿到自动机上跑,同时维护一下 ...

  2. BZOJ3413: 匹配(后缀自动机 线段树合并)

    题意 题目链接 Sol 神仙题Orz 后缀自动机 + 线段树合并... 首先可以转化一下模型(想不到qwq):问题可以转化为统计\(B\)中每个前缀在\(A\)中出现的次数.(画一画就出来了) 然后直 ...

  3. [Luogu5161]WD与数列(后缀数组/后缀自动机+线段树合并)

    https://blog.csdn.net/WAautomaton/article/details/85057257 解法一:后缀数组 显然将原数组差分后答案就是所有不相交不相邻重复子串个数+n*(n ...

  4. 模板—字符串—后缀自动机(后缀自动机+线段树合并求right集合)

    模板—字符串—后缀自动机(后缀自动机+线段树合并求right集合) Code: #include <bits/stdc++.h> using namespace std; #define ...

  5. 【BZOJ4556】[TJOI2016&HEOI2016] 字符串(后缀自动机+线段树合并+二分)

    点此看题面 大致题意: 给你一个字符串\(s\),每次问你一个子串\(s[a..b]\)的所有子串和\(s[c..d]\)的最长公共前缀. 二分 首先我们可以发现一个简单性质,即要求最长公共前缀,则我 ...

  6. bzoj5417/luoguP4770 [NOI2018]你的名字(后缀自动机+线段树合并)

    bzoj5417/luoguP4770 [NOI2018]你的名字(后缀自动机+线段树合并) bzoj Luogu 给出一个字符串 $ S $ 及 $ q $ 次询问,每次询问一个字符串 $ T $ ...

  7. CF 666E Forensic Examination——广义后缀自动机+线段树合并

    题目:http://codeforces.com/contest/666/problem/E 对模式串建广义后缀自动机,询问的时候把询问子串对应到广义后缀自动机的节点上,就处理了“区间”询问. 还要处 ...

  8. CF666E Forensic Examination(后缀自动机+线段树合并)

    给你一个串S以及一个字符串数组T[1..m],q次询问,每次问S的子串S[pl..pr]在T[l..r]中的哪个串里的出现次数最多,并输出出现次数. 如有多解输出最靠前的那一个. 我们首先对m个字符串 ...

  9. [CF666E]Forensic Examination:后缀自动机+线段树合并

    分析 用到了两个小套路: 使用线段树合并维护广义后缀自动机的\(right\)集合. 查询\(S[L,R]\)在\(T\)中的出现次数:给\(T\)建SAM,在上面跑\(S\),跑到\(R\)的时候先 ...

随机推荐

  1. JVM调优命令-jps

    JVM Process Status Tool,显示指定系统内所有的HotSpot虚拟机进程. 命令格式 1 jps [options] [hostid] options参数-l : 输出主类全名或j ...

  2. PHP 文件加密Zend Guard Loader 学习和使用(如何安装ioncube扩展对PHP代码加密)

    一.大体流程图 二.PHP 项目文件加密 下表列出了Zend产品中的PHP版本及其内部API版本和Zend产品版本. 如何加密请往后看 三.如何使用 第一步:确认当前环境 Amai Phalcon 前 ...

  3. 从数据库存储,文件结构谈到B树,散列

    昨天俱乐部内部办了一个讲座,关于常规数据库系统实现,听了之后有点混乱,于是花了很多时间特地查了一些资料,基本上自己感觉自己是明白了.特地写下来. 文章开头说明三点, 第一点,本文针对常规数据库,是为了 ...

  4. Flex 经验笔记一

    Module页面嵌套子Module页面直接用标签嵌入是不行的,无法显示出来,需要用到 ModuleManager 使用ModuleInfo 的 addEventListener 判断当子Module ...

  5. 【三分钟视频教程】iOS开发中 Xcode 报 apple-o linker 错误的#解决方案#

      [三分钟视频教程]iOS开发中 Xcode 报 apple-o linker 错误的#解决方案#   同样的道理,指向同一库文件的代码语句如果重复书写,即使重复书写所在的文件名字不同,同样会造成这 ...

  6. 第6月第4天 AVMutableComposition AVMutableVideoComposition

    1. AVMutableComposition is a mutable subclass of AVComposition you use when you want to create a new ...

  7. State Estimation for Robotics (Tim Barfoot) exercises Answers

    Here are some exercises answers for State Estimation for Robotics, which I did in June, 2017. The bo ...

  8. mybatis批量增加与删除——(十五)

    1.首先应该明白,mybatis增删改返回值是int型的影响行数的值 mapper接口 package cn.xm.mapper; import java.util.List; import cn.x ...

  9. Linux ALSA声卡驱动之五:移动设备中的ALSA(ASoC)

    转自http://blog.csdn.net/droidphone/article/details/7165482 1.  ASoC的由来 ASoC--ALSA System on Chip ,是建立 ...

  10. 安装Python3.6.4后,在使用numpy时报错RuntimeWarning: numpy.dtype size changed, may indicate binary incompatibility. Expected 96, got 88

    原因: 因为安装numpy用的是 pip来安装的 pypi官方对于numpy的库已经升级了,但是升级后的版本与其他的库不匹配 所以报错 解决: 先把已经安装的numpy卸载: pip uninstal ...