zoukankan      html  css  js  c++  java
  • Zipper_DFS

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 9554    Accepted Submission(s): 3397


    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
    #include<iostream>
    #include<string>
    #include<cstring>
    #include<stdio.h>
    using namespace std;
    int vis[201][201];
     string s1,s2,s3;
     int flag;
     int len1,len2,len3;
    void dfs(int a,int b,int c)
    {
        if(flag) return;
        if(c==len3)
        {
            flag=1;
            return ;
        }
        if(vis[a][b]) return;
        vis[a][b]=1;
        if(s1[a]==s3[c]) dfs(a+1,b,c+1);
        if(s2[b]==s3[c]) dfs(a,b+1,c+1);
    
    }
    int main()
    {
    
        int t;
        int cas=1;
        cin>>t;
        while(t--)
        {
            cin>>s1>>s2>>s3;
            printf("Data set %d: ",cas++);
            len1=s1.size();
            len2=s2.size();
            len3=s3.size();
            if(len1+len2!=len3)
            {
                cout<<"no"<<endl;
                continue;
            }
            memset(vis,0,sizeof(vis));
            flag=0;
            dfs(0,0,0);
            if(flag) cout<<"yes"<<endl;
            else cout<<"no"<<endl;
        }
        return 0;
    }
  • 相关阅读:
    【Neo4j cypher命令】 使用
    【Neo4j】 Neo4j数据插入的几种方式总结
    【Ubuntu】 解决 无法获得锁 /var/lib/dpkg/lock-frontend
    【python】win10配置pycharm 关于Error processing line 7 of D:softwaresAnaconda3libsite-packagespywin32.pth 问题的解决
    win10环境配置XShell6.0绿色版
    【python】 Anaconda3配置好后,启动jupyter服务异常的解决方法
    利用python+win32api获取标题对应的窗口句柄id,并且操作为当前活动窗口
    利用python+win32获取windows中任务中所有的窗口标题
    C++核心知识
    C++基础知识
  • 原文地址:https://www.cnblogs.com/iwantstrong/p/5756255.html
Copyright © 2011-2022 走看看