This page is still under construction.

Parts of this page are still being built. What you see may change.

Extending a Common Subsequence

Time limit1sMemory limit512 MB

Summary
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 XX = ababca, but not of YY = 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 XX and YY above, baa is a common subsequence of XX and YY, but aab is not.

Given a common subsequence WW of two sequences XX and YY, we want to decide whether WW is extendable. If inserting some element at some position of WW produces a longer common subsequence, WW is extendable; otherwise WW is not extendable. For example, for XX and YY above, the common subsequence baa can be extended to baba. The common subsequence ca, however, cannot be extended any further.

Given two sequences XX, YY and a common subsequence WW of the two sequences, write a program that decides whether WW is extendable.

Input

The first line contains the number of test cases TT.

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

Each test case consists of three lines, containing the sequences XX, YY, WW, 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 ∣X∣|X| and the sum of ∣Y∣|Y| are each at most 200 000200\,000.
  • WW is a common subsequence of XX and YY.
  • Sequences consist only of lowercase English letters.

Examples1

  1. Example 1

    Input
    2
    ababca
    cbabba
    baa
    aaabbbccc
    caacbbc
    ccc
    
    Expected output
    1
    0