QOJ.ac

QOJ

Límite de tiempo: 2 s Límite de memoria: 1024 MB Puntuación total: 100

#23308. Extending a Common Subsequence

Estadísticas

A sequence obtained by deleting zero or more elements from a sequence is called a subsequence of that sequence. For example, aab is a subsequence of $X=\texttt{ababca}$, but not of $Y=\texttt{cbabba}$.

Given two sequences, a subsequence appearing in both is called a common subsequence of the two sequences. For example, for the sequences $X$ and $Y$ above, baa is a common subsequence, but aab is not.

Given a common subsequence $W$ of two sequences $X$ and $Y$, we want to determine whether $W$ can be extended. We define $W$ to be extendable if inserting an element at some position in $W$ can produce a longer common subsequence; otherwise, it is not extendable. For example, for $X$ and $Y$ above, the common subsequence baa can be extended to baba. However, the common subsequence ca cannot be extended any further.

Given two sequences $X$, $Y$ and a common subsequence $W$ of them, write a program to determine whether $W$ is extendable.

Constraints

  • Each input contains at least $1$ and at most $100$ test cases to solve.
  • The sums of $|X|$ and $|Y|$ are each at most $200\,000$.
  • $W$ is a common subsequence of $X$ and $Y$.
  • The sequences contain only lowercase English letters.

Subtasks

  1. (14 points) $|W| = 1$
  2. (17 points) The sums of $|X|$ and $|Y|$ are each at most $300$.
  3. (25 points) The sums of $|X|$ and $|Y|$ are each at most $20\,000$.
  4. (44 points) No additional constraints.

Input

The first line contains the number of test cases $T$.

The next $3 \times T$ lines contain the test cases.

Each test case consists of three lines containing $X$, $Y$ and $W$, respectively.

Each sequence is given as consecutive lowercase English letters without spaces.

Output

For each test case, print whether it is extendable, one result per line.

Print 1 if it is extendable, and 0 otherwise.

Examples

Input 1

2
ababca
cbabba
baa 
aaabbbccc
caacbbc
ccc

Output 1

1
0

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.