Constraints
Time Limit: 1 secs, memory limit: 32 MB
Description
Given three strings, you are to determine whether the third string can be formed by combining the characters in the first two strings. the first two strings can be mixed arbitrarily, but each must stay in its original order. for example, consider forming "tcraete"
From "cat" and "Tree": string a: CAT string B: Tree string C: tcraete as you can see, we can form the third string by alternating characters from the two strings. as a second example, consider forming "catrtee" from "cat" and "Tree": string a: CAT string B:
Tree string C: catrtee Finally, notice that it is impossible to form "cttaree" from "cat" and "tree ".
Input
The first line of input contains a single positive integer from 1 through 1000. it represents the number of data sets to follow. the processing for each data set is identical. the data sets appear on the following lines, one data set per line. for each data
Set, the line of input consists of three strings, separated by a single space. all strings are composed of upper and lower case letters only. the length of the third string is always the sum of the lengths of the first two strings. the first two strings will
Have lengths between 1 and 200 characters, inclusive.
Output
For each data set, print: data set N: yes if the third string can be formed from the first two, or data set N: No if it cannot. of course n shocould be replaced by the data set number. see the sample output below for an example.
Sample Input
3cat tree tcraetecat tree catrteecat tree cttaree
Sample output
Data Set 1: yesdata set 2: yesdata set 3: No
Question Analysis:
If you use recursion or traversal to search, there will be overlapping subproblems. So we can use DP to do this. If X [I] [J] is set, it indicates that the I is obtained from the first string, and the J is obtained from the second string, and it is of the bool type.
The recursion is X [I] [J] = true if (X [I-1] [J] = true & X1 [I-1] = X3 [I-1 + J] | X [I] [J-1] = true & X2 [J-1] = X3 [J-1 + I])
Returns an array of X using two for loops.
# Include <iostream> # include <iomanip> # include <stdio. h> # include <cmath> # include <iomanip> # include <list> # include <map> # include <vector> # include <string> # include <algorithm> # include <sstream> # include <stack> # include <queue> # include <string. h> using namespace STD; int main () {int N; scanf ("% d", & N); For (INT xx = 0; XX <n; XX ++) {string x1, x2, X3; CIN> x1> X2> X3; bool data [201] [201]; int I = 0; for (I = 1; I <= x1.size (); I ++) {If (x1 [I-1] = X3 [I-1]) data [I] [0] = true; else break ;} for (; I <= x1.size (); I ++) data [I] [0] = false; for (I = 1; I <= x2.size (); I ++) {If (X2 [I-1] = X3 [I-1]) data [0] [I] = true; else break;} (; I <= x2.size (); I ++) data [0] [I] = false; for (I = 1; I <= x1.size (); I ++) {for (Int J = 1; j <= x2.size (); j ++) {If (data [I-1] [J] = true & X1 [I-1] = X3 [I-1 + J] | data [I] [J-1] = true & & X2 [J-1] = X3 [J-1 + I]) data [I] [J] = true; else data [I] [J] = false ;} //} cout <"Data Set" <xx + 1 <":"; if (data [x1.size ()] [x2.size ()]) cout <"yes" <Endl; else cout <"no" <Endl ;}}