zoukankan      html  css  js  c++  java
  • 【OpenJ_Bailian

    Zipper


    Descriptions:

    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

     题目链接:

    https://vjudge.net/problem/OpenJ_Bailian-2192

    题意:给定三个字符串,A,B,C。A和B的长度和等于C。判断字符串C能否由字符串A、B中的字符组成。要求,原来A 和 B 中的字符作为C中的字符时,还必须保持字符在原串中的顺序。

    假设 A中的前 i 个字符 和  B 中的前 j 个字符 可以构成 C中的前 i+j个字符。那么 如果A中的前 i+1 个字符和B中的前 j 个字符可以构成 C 中的前 i+j+1 个字符 或者 如果A中的前 i 个字符和B中的前 j+1 个字符可以构成 C 中的前 i+j+1 个字符。那么显然 C 中的前 i+j+1 个字符可由字符串A 和 字符串 B 中的前 i+j+1 个字符构成。没啥说的直接上代码吧

    AC代码

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <fstream>
     4 #include <algorithm>
     5 #include <cmath>
     6 #include <deque>
     7 #include <vector>
     8 #include <queue>
     9 #include <string>
    10 #include <cstring>
    11 #include <map>
    12 #include <stack>
    13 #include <set>
    14 using namespace std;
    15 string s1,s2,s3;
    16 int cnt1,cnt2,cnt3;
    17 int len1,len2,len3;
    18 bool dp[1005][1005];
    19 bool f=0;//建一个flag,判断输出yes/no
    20 void dfs(int t1,int t2,int t3)//深搜
    21 {
    22     if(f)
    23         return;
    24     if(t1==len1&&t2==len2)
    25     {
    26         f=1;
    27         return;
    28     }
    29     if(dp[t1][t2]==1)
    30         return;
    31     if(s1[t1]==s3[t3])
    32     {
    33         dp[t1][t2]=1;
    34         dfs(t1+1,t2,t3+1);
    35     }
    36     if(s2[t2]==s3[t3])
    37     {
    38         dp[t1][t2]=1;
    39         dfs(t1,t2+1,t3+1);
    40     }
    41 }
    42 int main()
    43 {
    44     int sum=0;
    45     int T;
    46     cin >> T;
    47     while(T--)
    48     {
    49         f=0;
    50         sum++;
    51         memset(dp,0,sizeof(dp));//必须初始化
    52         cin >> s1 >> s2 >> s3;
    53         len1=s1.length();
    54         len2=s2.length();
    55         len3=s3.length();
    56         dfs(0,0,0);
    57         if(f)
    58             printf("Data set %d: yes
    ",sum);
    59         else
    60             printf("Data set %d: no
    ",sum);
    61     }
    62 }
  • 相关阅读:
    用户登陆显示cpu、负载、内存信息
    递归算法总结
    Java算法之递归打破及在真实项目中的使用实例
    史上最简单,一步集成侧滑(删除)菜单,高仿QQ、IOS。
    仿饿了么购物车下单效果
    一起来写个酷炫的水波纹浪啊浪界面
    一行实现QQ群组头像,微信群组,圆角等效果. 并支持url直接加载图片
    使用 CoordinatorLayout 实现复杂联动效果
    这交互炸了(三) :不看后悔!你一定没见过这样的闪屏
    这交互炸了(二):爱范儿是如何让详情页缩小为横向列表的
  • 原文地址:https://www.cnblogs.com/sky-stars/p/10944278.html
Copyright © 2011-2022 走看看