CF1801G.A task for substrings

NOI/NOI+/CTSC

通过率:0%

时间限制:4.00s

内存限制:1024MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Philip is very fond of tasks on the lines. He had already solved all the problems known to him, but this was not enough for him. Therefore, Philip decided to come up with his own task.

To do this, he took the string tt and a set of nn strings s1s_1, s2s_2, s3s_3, ..., sns_n. Philip has mm queries, in the iith of them, Philip wants to take a substring of the string tt from lil_ith to rir_ith character, and count the number of its substrings that match some string from the set. More formally, Philip wants to count the number of pairs of positions aa, bb, such that li≤a≤b≤ril_i \le a \le b \le r_i, and the substring of the string tt from aath to bbth character coincides with some string sjs_j from the set.

A substring of the string tt from aath to bbth character is a string obtained from tt by removing the a−1a - 1 character from the beginning and ∣t∣−b|t| - b characters from the end, where ∣t∣|t| denotes the length of the string tt.

Philip has already solved this problem, but can you?

菲利普非常喜欢与字符串子串相关的问题。他早已解决了所有已知的此类题目,但这仍不能满足他。因此,菲利普决定自己构造一道新题。

为此,他取了一个字符串 tt 和一个包含 nn 个字符串的集合 s1s_1, s2s_2, s3s_3, ..., sns_n。菲利普共有 mm 个查询;在第 ii 个查询中,他希望取出字符串 tt 中从第 lil_i 个字符到第 rir_i 个字符的子串,并统计该子串中有多少个子串与集合中的某个字符串匹配。更准确地说,菲利普希望统计满足如下条件的位置对 (a,b)(a, b) 的数量:li≤a≤b≤ril_i \le a \le b \le r_i,且字符串 tt 中从第 aa 个字符到第 bb 个字符构成的子串与集合中的某个字符串 sjs_j 完全相同。

字符串 tt 中从第 aa 个字符到第 bb 个字符的子串,是指从 tt 中删去开头 a−1a - 1 个字符、结尾 ∣t∣−b|t| - b 个字符后所得的字符串,其中 ∣t∣|t| 表示字符串 tt 的长度。

菲利普已经解决了这个问题,但你能吗?

输入格式

The first line contains two positive integers nn and mm (1≤n,m≤500 0001 \le n, m \le 500\,000) — the number of rows in the set and the number of queries.

The second line contains a single string tt consisting of lowercase letters of the English alphabet (1≤∣t∣≤5⋅1061 \le |t| \le 5 \cdot 10^6).

The following nn lines describe the strings from the set. In the iith of them, a single string sis_i is given, consisting of lowercase letters of the English alphabet. Denote by SS the total length of all strings from the set. It is guaranteed that S≤106S \le 10^6, as well as that all strings of sis_i are different.

In the following lines, queries are entered. The iith of them contains two positive integers lil_i and rir_i (1≤li≤ri≤∣t∣1 \le l_i \le r_i \le |t|) — the left and right border of the substring tt from the ii-th query.

第一行包含两个正整数 nn 和 mm(1≤n,m≤500 0001 \le n, m \le 500\,000)—— 分别表示字符串集合的行数(即字符串个数)和查询次数。

第二行包含一个仅由小写英文字母组成的字符串 tt(1≤∣t∣≤5⋅1061 \le |t| \le 5 \cdot 10^6)。

接下来的 nn 行描述集合中的各个字符串。第 ii 行给出一个字符串 sis_i,它也仅由小写英文字母组成。记 SS 为集合中所有字符串的总长度。保证 S≤106S \le 10^6,且所有字符串 sis_i 互不相同。

随后若干行输入查询。第 ii 个查询包含两个正整数 lil_i 和 rir_i(1≤li≤ri≤∣t∣1 \le l_i \le r_i \le |t|)—— 分别表示第 ii 个查询中字符串 tt 的子串的左边界和右边界。

输出格式

In a single line, print mm integers, iith of them should be equal to the answers to the iith query.

在一行中输出 mm 个整数,其中第 ii 个整数应等于第 ii 个查询的答案。

输入输出样例

  • 输入#1

    3 5
    abacaba
    aba
    a
    ac
    1 7
    1 3
    2 7
    2 5
    4 5

    输出#1

    7 3 5 3 1
  • 输入#2

    4 4
    abcdca
    ab
    ca
    bcd
    openolympiad
    1 5
    2 2
    2 6
    1 6

    输出#2

    2 0 2 3

说明/提示

In the first example, the first query requires the entire string to count the number of substrings that are included in the set. The substrings [1,3][1, 3] and [4,6][4, 6] coincide with the string "aba". The substrings match with the string "a" [1,1][1, 1], [3,3][3, 3], [5,5][5, 5], [7,7][7, 7]. The substring [3,4][3, 4] matches the string "ac". In total, it turns out that 7 substrings of the string "abacaba" match the strings from the set.

In the second query, a substring from position 1 to position 3 is taken from the source string, this is the string "aba". The string "aba" enters it 1 time, the string "a" enters it 2 times and the string "ac" does not enter it once as a substring. In the third query, a substring from the 2nd to the 7th position is taken from the source string, this is the string "bacaba". The string "aba" is included in it 1 time, the string "a" is included 3 times and the string "ac" is included 1 time as a substring.

在第一个例子中,第一个查询要求对整个字符串进行处理,以统计其中属于给定集合的子串数量。子串 [1,3][1, 3] 和 [4,6][4, 6] 均与字符串 "aba" 完全一致;子串 [1,1][1, 1]、[3,3][3, 3]、[5,5][5, 5]、[7,7][7, 7] 均与字符串 "a" 匹配;子串 [3,4][3, 4] 与字符串 "ac" 匹配。总计,字符串 "abacaba" 中有 7 个子串与集合中的字符串匹配。

在第二个查询中,从原字符串中取出位置 1 到位置 3 的子串,即字符串 "aba"。其中,字符串 "aba" 作为子串出现 1 次,字符串 "a" 出现 2 次,而字符串 "ac" 未出现(即出现 0 次)。

在第三个查询中,从原字符串中取出第 2 至第 7 位的子串,即字符串 "bacaba"。其中,字符串 "aba" 作为子串出现 1 次,字符串 "a" 出现 3 次,字符串 "ac" 出现 1 次。

输入解题思路,AI测评打分。不知道怎么写?

首页