Subsequence Queries

5

1 votes
Algorithms, Medium, Sqrt-Decomposition, Square Root Decomposition, String Manipulation
Problem

You are given \(N\) strings and \(Q\) questions. Each question contains two values \(X\) and \(Y\). Print "Yes" if the \(X^{th}\) string is a subsequence of \(Y^{th}\) string or Yth string is a subsequence of Xth string, otherwise,print "No".

subsequence is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements. Read more about subsequences here.

Input format 

  • First line: Two space-separated integers \(N\) and \(Q\)
  • Next N lines: \(N\) strings
  • Next Q lines: Two space-separated integers \(X\) and \(Y\)

Output format

Print the answer to each question in a new line.

Constraints

\(1 \le |S_i| \le 10^6\)

\(1 \le N \le 10^5\)

\(N \le\sum_{i=1}^{n}|S_i|\le 10^6 \)

\(1 \le Q \le 10^5\)

\(1 \le X,Y \le N\)

\(|Si|\) denotes length of ith string

Each string contains only lowercase alphabets.

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

1. acd is a subsequence of abcd.

2. acd is NOT a subsequence of bcf or vice versa.

3. ad is a subsequence of abcd.

4. ad is a subsequence of acd.

Editor Image

?