zoukankan      html  css  js  c++  java
  • DFS专题 Zipper

    这道题放在 DFS 里不太合适,我刚开始就想到了 DP 却没写,DFS 超时后看了题解才发现就是记忆化搜索(白书认为记忆化就是DP);

    1TLE 2WA,把 lens 写成了 lent 后来才发现的。

    View Code
    # include <cstdio>
    # include <cstring>
    
    # define N 200 + 10
    
    bool finished, f[N][N];
    int lens, lent;
    char s[N], t[N], g[2 * N];
    
    void dfs(int p, int q, int cnt)
    {
        if (finished) return;
        if (cnt == lens+lent) {finished = true; return ;}
        if (f[p][q]) return ;
        if (p<lens && s[p] == g[cnt]) dfs(p+1, q, cnt+1);
        if (q<lent && t[q] == g[cnt]) dfs(p, q+1, cnt+1);
        f[p][q] = true;
    }
    
    int main()
    {
        int T;
    
        scanf("%d", &T);
        for (int i = 1; i <= T; ++i)
        {
            scanf("%s%s%s", s, t, g);
            lens = strlen(s), lent = strlen(t);
            memset(f, false, sizeof(f));
            printf("Data set %d: ", i);
            finished = false;
            dfs(0, 0, 0);
            puts(finished ? "yes":"no");
        }
    
        return 0;
    }

    Problem 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 should be replaced by the data set number. See the sample output below for an 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
    
  • 相关阅读:
    Sign APK without putting keystore info in build.gradle
    Sign APK without putting keystore info in build.gradle
    Sketch教程
    Sketch教程
    简要分析unity3d中剪不断理还乱的yield
    简要分析unity3d中剪不断理还乱的yield
    iOS开发系列--地图与定位总结
    iOS开发系列--地图与定位总结
    Launch Screen在iOS7/8中的实现
    linux 命令随笔 ls cd pwd mkdir rm mv cp cat nl
  • 原文地址:https://www.cnblogs.com/JMDWQ/p/2598387.html
Copyright © 2011-2022 走看看