题目收集
题目列表题单
返回列表

Finding Cyclic Isomorphism

CodeforcesP0041· 4000ms· 1024 MB· ZJU Summer 2026 Contest 4原题链接
字符串后缀自动机

创建于 2026/07/17 · 更新于 2026/07/17

题目描述

Colin is learning isomorphism theory recently and tries to apply it to string problems!

Define two strings S,TS, TS,T to be cyclically isomorphic if and only if they satisfy all the following conditions:

  • their lengths are equal, i.e., ∣S∣=∣T∣|S|=|T|∣S∣=∣T∣;
  • there exists an integer i∈[0,∣S∣)i\in[0,|S|)i∈[0,∣S∣) such that if we cyclically right-shift SSS for iii times, the resulting string S′=TS'=TS′=T.

For example, strings abcb and babc are cyclically isomorphic since we can cyclically right-shift the first string once to get the second string.

Now you are given nnn non-empty pattern strings S1,S2,⋯ ,SnS_1,S_2,\cdots,S_nS1​,S2​,⋯,Sn​, and another non-empty text string TTT.

Colin raises qqq questions; each question is described by an interval [li,ri] (1≤li<ri≤∣T∣)[l_i, r_i]~(1\le l_i \lt r_i\le |T|)[li​,ri​] (1≤li​<ri​≤∣T∣). Colin wants to know how many integers xxx satisfy all the following conditions:

  • x∈[li,min⁡(∣T∣+li−ri+1,ri−1)]x\in [l_i,\min(|T|+l_i-r_i+1,r_i-1)]x∈[li​,min(∣T∣+li​−ri​+1,ri​−1)]
  • T[li,x−1]=T[ri,x+ri−li−1]T[l_i,x-1]=T[r_i,x+r_i-l_i-1]T[li​,x−1]=T[ri​,x+ri​−li​−1]
  • T[x,ri−1]T[x,r_i-1]T[x,ri​−1] is cyclically isomorphic with at least one of S1,S2,⋯ ,SnS_1,S_2,\cdots,S_nS1​,S2​,⋯,Sn​.

Recall that ∣T∣|T|∣T∣ represents the length of string TTT, and T[a,b] (1≤a≤b≤∣T∣)T[a, b]~(1\le a\le b\le |T|)T[a,b] (1≤a≤b≤∣T∣) represents the substring of TTT from the aaa-th character to the bbb-th character(including both boundaries). T[a,b] (a>b)T[a, b]~(a \gt b)T[a,b] (a>b) represents an empty string, and empty strings are always equal.

输入格式

The first line contains two integers n,q (1≤n≤5×105,1≤q≤106)n,q~(1\le n\le 5\times 10^5, 1\le q\le 10^6)n,q (1≤n≤5×105,1≤q≤106), representing the number of pattern strings and the number of questions.

For the following nnn lines, each line contains a non-empty string SiS_iSi​ consisting of lowercase letters, representing the iii-th pattern string. It's guaranteed that ∑i=1n∣Si∣≤5×105\sum_{i=1}^n |S_i|\le 5\times 10^5∑i=1n​∣Si​∣≤5×105.

The following line contains a non-empty string T (2≤∣T∣≤106)T~(2\le |T|\le 10^6)T (2≤∣T∣≤106) consisting of lowercase letters.

For the following qqq lines, each line contains two integers li,ri (1≤li<ri≤∣T∣)l_i, r_i~(1\le l_i \lt r_i\le |T|)li​,ri​ (1≤li​<ri​≤∣T∣), describing the iii-th question.

输出格式

Output qqq lines; the iii-th line contains a single integer, representing the answer for the iii-th question.

样例输入 #1

5 5
baab
aa
aaab
ab
abaaa
bbaabababb
7 9
1 4
3 7
7 9
5 10

样例输出 #1

1
0
1
1
0