PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#SRD0000133. Truy vấn dãy con của xâu (Subsequence queries)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Truy vấn dãy con của xâu (Subsequence queries)

Subsequence queries

Source: MarisaOJ

Version: Phuoc Hung OJ Extended

Problem Statement

You are given nn strings S1,S2,…,SnS_1,S_2,\ldots,S_n. String AA is a subsequence of string BB if some characters of BB, at strictly increasing positions, form AA. The selected positions need not be contiguous. For example, abc is a subsequence of axbyc, while acb is not.

For each of qq independent queries containing indices x,yx,y, determine whether at least one of these statements is true:

  • SxS_x is a subsequence of SyS_y.
  • SyS_y is a subsequence of SxS_x.

Equal strings always satisfy the requirement. Character order must be preserved, although characters may be skipped.

Input

  • The first line contains integers n,qn,q, the number of strings and queries.
  • The next nn lines contain the strings; line ii contains SiS_i.
  • Each of the following qq lines contains indices xj,yjx_j,y_j of the strings in query jj.

Output

Print one line per query in the original order. Print YES if either string is a subsequence of the other, otherwise print NO.

Subtasks

  • Subtask 1 (10 points): 1≤n,q≤201\le n,q\le20; 1≤∑∣Si∣≤2001\le\sum |S_i|\le200; each SiS_i is nonempty and contains only lowercase English letters; 1≤x,y≤n1\le x,y\le n in every query.
  • Subtask 2 (20 points): 1≤n,q≤1 0001\le n,q\le1\,000; 1≤∑∣Si∣≤30 0001\le\sum |S_i|\le30\,000; each SiS_i is nonempty and contains only lowercase English letters; 1≤x,y≤n1\le x,y\le n in every query.
  • Subtask 3 (30 points): 1≤n,q≤30 0001\le n,q\le30\,000; 1≤∑∣Si∣≤200 0001\le\sum |S_i|\le200\,000; each SiS_i is nonempty and contains only lowercase English letters; 1≤x,y≤n1\le x,y\le n in every query.
  • Subtask 4 (40 points): 1≤n,q≤1051\le n,q\le10^5; 1≤∑∣Si∣≤1061\le\sum |S_i|\le10^6; each SiS_i is nonempty and contains only lowercase English letters; 1≤x,y≤n1\le x,y\le n in every query.

Examples

Example 1

Input

3 3
aab
db
dacb
2 1
2 3
1 3

Output

NO
YES
NO

Explanation

For (2,1)(2,1), db contains d, which is absent from aab; the reverse direction is impossible as well. For (2,3)(2,3), select positions 11 and 44 of dacb to obtain db, so the answer is YES. For (1,3)(1,3), aab requires two a characters before b, but dacb has only one a; the opposite direction also fails because dacb is longer.

Example 2

Input

4 4
ab
aba
bbb
babbb
1 2
1 3
3 4
2 4

Output

YES
NO
YES
NO

Explanation

ab is a subsequence of aba. Neither ab nor bbb is a subsequence of the other. bbb is a subsequence of babbb by selecting three b characters. aba is not a subsequence of babbb because there is only one a; the reverse direction also fails by length, so the fourth answer is NO.