Subsequence queries - MarisaOJ: Marisa Online Judge

Subsequence queries

Time limit: 4000 ms
Memory limit: 256 MB
Given $n$ strings $S_1, S_2, ..., S_n$. There are $q$ queries, each consisting of two integers $x, y$. Check whether $S_x$ is a substring (not necessarily contiguous) of $S_y$ or if $S_y$ is a substring (not necessarily contiguous) of $S_x$. ### Input - The first line contains two integers $n, q$. - The next $n$ lines, where the $i$-th line represents the string $S_i$. - The next $q$ lines, each containing two integers $x, y$. ### Output - For each query, print `YES` if $S_x$ is a substring of $S_y$ or if $S_y$ is a substring of $S_x$, otherwise print `NO`. ### Constraints - $1 \le n, q \le 10^5$. - $1 \le \sum |S_i| \le 10^6$ - $1 \le x, y \le n$. - All strings consist only of lowercase letters. ### Example Input: ``` 3 3 aab db dacb 2 1 2 3 1 3 ``` Output: ``` NO YES NO ```