Palindrome Strings

시간 제한2초메모리 제한2048 MB

요약
고정된 문자열 S와 q개의 질의 문자열 t가 주어질 때, t 뒤에 S[l..r]을 이어 붙인 문자열이 회문이 되는 (l, r) 쌍의 개수를 각 질의마다 구한다.
난이도

어려움10점 중 9점

유형
문자열 매칭, 문자열, 해시맵, 누적 합
정답자
아직 제출이 없습니다

문제

You are given a string S=S_1S_2…S_∣S∣S = S\_1 S\_2 \ldots S\_{|S|} and qq queries. In each query, a string t=t_1t_2…t_∣t∣t = t\_1 t\_2 \ldots t\_{|t|} is given, and you should determine the number of pairs (ℓ,r)(\ell, r) such that 1≤ℓ≤r≤∣S∣1 \le \ell \le r \le |S| and the combined string t_1t_2…t_∣t∣S_ℓS_ℓ+1…S_rt\_1 t\_2 \ldots t\_{|t|} S\_{\ell} S\_{\ell + 1} \ldots S\_r is a palindrome, which means that t_1t_2…t_∣t∣S_ℓS_ℓ+1…S_r=S_rS_r−1…S_ℓt_∣t∣t_∣t∣−1…t_1.t\_1 t\_2 \ldots t\_{|t|} S\_{\ell} S\_{\ell + 1} \ldots S\_r = S\_r S\_{r - 1} \ldots S\_{\ell} t\_{|t|} t\_{|t| - 1} \ldots t\_1\text{.}

입력

The first line contains two integers nn and qq (1≤n≤1061 \le n \le 10^6, 1≤q≤1051 \le q \le 10^5) denoting the length of string SS and the number of queries, respectively.

The second line contains a single string SS.

Each of the following qq lines contains a single string tt denoting a query.

It is guaranteed that all the strings only contain lowercase English letters and that ∑∣t∣≤106\sum |t| \le 10^6.

출력

For each query, output a single line containing one integer: the required number of pairs.

힌트

  • For the first query, the 4 pairs are (2,3)(2, 3), (3,3)(3, 3), (7,8)(7, 8), and (8,8)(8, 8), and the combined strings are "pcp", "pp", "pmp", "pp", respectively.
  • For the second query, the 7 pairs are (1,2)(1, 2), (2,2)(2, 2), (2,5)(2, 5), (3,4)(3, 4), (4,4)(4, 4), (4,5)(4, 5), and (5,5)(5, 5).
  • For the third query, the 4 pairs are (1,3)(1, 3), (2,3)(2, 3), (3,3)(3, 3), and (8,8)(8, 8).

예제1

  1. 예제 1

    입력
    8 3
    icpccamp
    p
    c
    pc
    
    예상 출력
    4
    7
    4