Extending a Common Subsequence
Time limit1sMemory limit512 MB
Given strings X, Y and a common subsequence W, decide whether some character can be inserted into W to get a longer common subsequence of X and Y.
- Level
Medium7 of 10
- Topics
- String, Dynamic programming, Greedy, Two pointers
- Solved
- No attempts yet
Problem
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 = ababca, but not of = cbabba.
A subsequence that appears in both of two sequences is called a common subsequence of the two sequences. For example, for the two sequences and above, baa is a common subsequence of and , but aab is not.
Given a common subsequence of two sequences and , we want to decide whether is extendable. If inserting some element at some position of produces a longer common subsequence, is extendable; otherwise is not extendable. For example, for and above, the common subsequence baa can be extended to baba. The common subsequence ca, however, cannot be extended any further.
Given two sequences , and a common subsequence of the two sequences, write a program that decides whether is extendable.
Input
The first line contains the number of test cases .
The next lines contain the test cases.
Each test case consists of three lines, containing the sequences , , , one per line.
Each sequence is given as a contiguous string of lowercase English letters with no spaces.
Output
For each test case, print on its own line whether the sequence is extendable.
Print 1 if it is extendable, and 0 otherwise.
Constraints
- One input file contains between 1 and 100 test cases.
- The sum of and the sum of are each at most .
- is a common subsequence of and .
- Sequences consist only of lowercase English letters.