E. Forensic Examination
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 l, r, pl, pr (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.
input
suffixtree
3
suffixtreesareawesome
cartesiantreeisworsethansegmenttree
nyeeheeheee
2
1 2 1 10
1 3 9 10
output
1 1
3 4
题目大意:
给定一个字符串$s$,和$m$个字符串$p_1…….p_m$,$q$次询问,每次询问在$p_l……p_r$中,$s$的一个子串$s[x…y]$在哪一个串中出现次数最多。
询问子串的出现次数,考虑构建后缀自动机,由于有多个串,因此需要分开维护$Right$集合的大小,构建串$s+@+p_1+@+……+p_m$的后缀自动机。对于每个节点,维护一颗线段树,表示该节点表示的子串在每一个$p$中出现的次数,这个在每次添加新的$np$节点时直接往线段树中插入就行。
自动机建好了之后按照$parent$树,自底向上将子节点的线段树合并到父节点上,利用可持久化线段树可以完成。
然后对于每次询问,预先记录每个$s$每个前缀在自动机上的位置,然后在$parent$树上倍增查找子串对应节点,找到对应节点后直接在线段树上询问区间$[l,r]$的最大值即可。
总时间复杂度$O(nlog^2n)$,注意在合并线段树的时候一定要新开节点。
同时这题有个坑点就是当出现次数是$0$时,直接输出查询的左界。
代码:
1 |
|