CF1801G.A task for substrings

普及/提高-

通过率:0%

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 ii th of them, Philip wants to take a substring of the string tt from lil_i th to rir_i th 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 liabril_i \le a \le b \le r_i , and the substring of the string tt from aa th to bb th character coincides with some string sjs_j from the set.

A substring of the string tt from aa th to bb th character is a string obtained from tt by removing the a1a - 1 character from the beginning and tb|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?

输入格式

The first line contains two positive integers nn and mm ( 1n,m5000001 \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 ( 1t51061 \le |t| \le 5 \cdot 10^6 ).

The following nn lines describe the strings from the set. In the ii th 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 S106S \le 10^6 , as well as that all strings of sis_i are different.

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

输出格式

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

输入输出样例

  • 输入#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.

首页