#C2949. Subsequence Checker

    ID: 46321 Type: Default 1000ms 256MiB

Subsequence Checker

Subsequence Checker

You are given two strings, s and p. The task is to determine whether p is a subsequence of s. A string p is a subsequence of s if p can be derived from s by deleting zero or more characters without changing the order of the remaining characters.

Example:

  • If s = "abcde" and p = "ace", the answer is YES.
  • If s = "abcde" and p = "aec", the answer is NO.

The solution should implement an efficient algorithm with a two-pointer technique. Mathematically, the condition for p to be a subsequence of s can be stated as: \[ \exists\, 0 \leq i_1 < i_2 < \cdots < i_k < |s| \quad \text{such that} \quad s[i_1] = p_1, s[i_2] = p_2, \ldots, s[i_k] = p_k \] where \( p = p_1p_2\ldots p_k \).

inputFormat

The first line contains an integer T representing the number of test cases. For each test case, there are two lines:

  • The first line contains the string s.
  • The second line contains the string p.

outputFormat

For each test case, output a single line containing YES if p is a subsequence of s, or NO otherwise.

## sample
3
abcde
ace
abcde
aec
axc
bx
YES

NO NO

</p>