"Memory Search" HDU-1501 zipper

Source: Internet
Author: User
Tags sca
Zipper Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)Problem Description Given Three strings, you are to determine whether the third string can is formed by combining the Char Acters in the the two strings. The the 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, we can form the third string by alternating characters to 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's impossible to form "cttaree" from "Cat" and "tree".

Input the ' a ' 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 of the data set per line.

For each data set, the line of input consists of three strings, separated by a. All strings are composed of the upper and lower case letters only. The length of the third string is always the sum of the lengths of the the ' the ' the ' the ' the ' the ' the ' the The two strings would have lengths between 1 and characters, inclusive.


Output for each data set, print:

Data Set N:yes

If the third string can be formed from the two, or

Data Set N:no

If it cannot. Of course n should be replaced by the data set number. The sample output below for a example.

Sample Input
3 Cat Tree Tcraete cat tree catrtee cat Tree Cttaree
Sample Output
Data set 1:yes data set 2:yes data set 3:no   ————————————————————————————————————————————————————————: given three strings, Ask if the third string can be pieced together from the first two strings. Order cannot be disturbed. Train of thought: look like a Dfs. But it needs to be memorized. Why should we remember it. First we are the C string has been matched to the last time to get OK = true, after the process of backtracking once OK = True also directly return (pruning). Then if OK = false, even if the state is searched, it will be searched again. Why is there a state of search? If a string is similar to the beginning of a string B, it is a character x, assuming that the x of a string is used up, the number of x that the A string and B string contributes to the C string must be adjusted retrospectively. After that, there will be a time when a string x is used up and the number of characters in the B string is the same. However, this state has been searched. Refer to an example:aaabb  AAAAAACD AAAAAACAAABBD code as follows:
/** * ID:J.SURE.1 * PROG: * lang:c++/#include <cstdio> #include <cstdlib> #include <cstring> #i Nclude <algorithm> #include <ctime> #include <cmath> #include <stack> #include <queue> # Include <vector> #include <map> #include <set> #include <string> #include <climits> # Include <iostream> #define for (i, X, y) for (int i=x; i<y; i++) #define FOR_ (i, x, y) for (int i=x; i>=y; i--) # Define MEM (f, X) memset (f, X, sizeof (f)) #define SCA (x) scanf ("%d", &x) #define PRI (x) printf ("%d\n", x) #define LL Lo
Ng long using namespace std;
const int INF = 0X3F3F3F3F;
/****************************************/const int N = 222;
Char A[n], b[n], c[n<<1];
BOOL Vis[n][n];
int n;

BOOL OK;
        void Dfs (int u, int ai, int bi) {if (U = n) {ok = true;
    return; } if (ok| |
    VIS[AI][BI]) return;
    VIS[AI][BI] = true;
 if (C[u]!= A[ai] && c[u]!= B[bi]) {return;   } if (c[u] = = A[ai]) dfs (u+1, ai+1, BI);
if (c[u] = = B[bi]) dfs (u+1, AI, bi+1);
    int main () {#ifdef j_sure freopen ("000.in", "R", stdin);
Freopen ("999.out", "w", stdout);
    #endif int T, Kase = 1;
    Sca (T);
        while (t--) {scanf ("%s%s%s", A, B, c);
        Mem (Vis, 0);
        n = strlen (c);
        OK = false;
        DFS (0, 0, 0); printf ("Data set%d:%s\n", kase++, OK?)
    "Yes": "No");
return 0; }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.