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
    
  • 相关阅读:
    Windows 2008 R2 远程桌面服务(八)远程桌面服务器安全设置
    从硬盘上安装SQLServer2005的问题
    在 Windows server 2008 下计划任务无法正常执行bat批处理文件
    两部搞定windows server 2008 R2 中IE8的增强安全配置功能
    Microsoft SQL Server 2005 Service Pack 4 RTM
    学习地址
    Windows 2008 远程桌面如何设置两个用户共享一个会话
    Windows Server 2008 启用无线网卡
    远程桌面连接指定会话(Session)
    固定宽度弹性布局(以适应各种各辨率)
  • 原文地址:https://www.cnblogs.com/JMDWQ/p/2598387.html
Copyright © 2011-2022 走看看