zoukankan      html  css  js  c++  java
  • 1501 Zipper(dp或dfs)

    Zipper

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


    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
     
     1 #include <iostream>
     2 #include <cstring>
     3 #include <cstdio>
     4 #include <algorithm>
     5 
     6 using namespace std;
     7 
     8 int n;
     9 string s1,s2,res;
    10 int len1,len2,len3;
    11 int dp[205][205],vis[205][205];
    12 bool flag;
    13 
    14 void dfs(int a,int b,int c){
    15     if(c==len3) {flag=true;return;}
    16 
    17     if(vis[a][b]) return;
    18     vis[a][b]=1;
    19 
    20     if(s1[a]==res[c])  dfs(a+1,b,c+1);  //两种情况都要算一下
    21     if(s2[b]==res[c])  dfs(a,b+1,c+1);
    22 
    23 }
    24 
    25 int main(){
    26     ios::sync_with_stdio(false);
    27     int jishu=0;
    28     cin>>n;
    29     while(n--){
    30         memset(vis,0,sizeof(vis));
    31         flag=false;
    32         cin>>s1>>s2>>res;
    33         len1=s1.size(),len2=s2.size(),len3=res.size();
    34         dfs(0,0,0);
    35         if(flag) cout << "Data set " << ++jishu << ": yes" << endl;
    36         else cout << "Data set " << ++jishu << ": no" << endl;
    37     }
    38     return 0;
    39 }
    dfs
     1 #include <iostream>
     2 #include <cstring>
     3 #include <cstdio>
     4 #include <algorithm>
     5 
     6 using namespace std;
     7 
     8 int n;
     9 string s1,s2,res;
    10 int len1,len2,len3;
    11 int dp[205][205],vis[205][205];
    12 bool flag;
    13 
    14 int main(){
    15     ios::sync_with_stdio(false);
    16     int jishu=0;
    17     cin>>n;
    18     while(n--){
    19         cin>>s1>>s2>>res;
    20         len1=s1.size(),len2=s2.size(),len3=res.size();
    21         s1=" "+s1;s2=" "+s2;res=" "+res;
    22         for(int i=1;i<=len1;i++){
    23             dp[i][0]=s1[i]==res[i]?1:0;
    24         }
    25         for(int i=1;i<=len2;i++){
    26             dp[0][i]=s2[i]==res[i]?1:0;
    27         }
    28         for(int i=1;i<=len1;i++){
    29             for(int j=1;j<=len2;j++){
    30                 dp[i][j]=((dp[i-1][j]&&s1[i]==res[i+j])||(dp[i][j-1]&&s2[j]==res[i+j]));
    31             }
    32         }
    33         cout << "Date set " << ++jishu;
    34         if(dp[len1][len2]) cout << ": yes" << endl;
    35         else cout << ": no" << endl;
    36     }
    37     return 0;
    38 
    39 }
    dp
  • 相关阅读:
    web全栈第四讲:后端框架express.js初步体验。
    web全栈第三讲:Angular单页面应用
    Web第六讲:HTML基础
    关于命令模式的一点意见
    黑猫白猫读《大话设计模式》
    排序二叉树的实现和我的一个数据结构设计
    《算法与数据结构C语言描述》优先队列
    《算法与数据结构C语言描述》里的农夫过河
    读《算法与数据结构c语言描述》
    《算法与数据结构C语言描述》里的求多项达式值
  • 原文地址:https://www.cnblogs.com/qq-1585047819/p/11801316.html
Copyright © 2011-2022 走看看