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代码

 #include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
using namespace std;
string s1,s2,s3;
int cnt1,cnt2,cnt3;
int len1,len2,len3;
bool dp[][];
bool f=;//建一个flag,判断输出yes/no
void dfs(int t1,int t2,int t3)//深搜
{
if(f)
return;
if(t1==len1&&t2==len2)
{
f=;
return;
}
if(dp[t1][t2]==)
return;
if(s1[t1]==s3[t3])
{
dp[t1][t2]=;
dfs(t1+,t2,t3+);
}
if(s2[t2]==s3[t3])
{
dp[t1][t2]=;
dfs(t1,t2+,t3+);
}
}
int main()
{
int sum=;
int T;
cin >> T;
while(T--)
{
f=;
sum++;
memset(dp,,sizeof(dp));//必须初始化
cin >> s1 >> s2 >> s3;
len1=s1.length();
len2=s2.length();
len3=s3.length();
dfs(,,);
if(f)
printf("Data set %d: yes\n",sum);
else
printf("Data set %d: no\n",sum);
}
}

【OpenJ_Bailian - 2192】Zipper(dfs)的更多相关文章

  1. 【POJ - 1950】Dessert(dfs)

    -->Dessert Descriptions: 给你一个数N(3<=N<=15);每个数之间有三种运算符“‘+’,‘-’,‘.’”.输出和值等于零的所有的运算情况及次数num,如果 ...

  2. 【OpenJ_Bailian - 2795】金银岛(贪心)

    金银岛 Descriptions: 某天KID利用飞行器飞到了一个金银岛上,上面有许多珍贵的金属,KID虽然更喜欢各种宝石的艺术品,可是也不拒绝这样珍贵的金属.但是他只带着一个口袋,口袋至多只能装重量 ...

  3. 【Aizu - 0525】Osenbei (dfs)

    -->Osenbei 直接写中文了 Descriptions: 给出n行m列的0.1矩阵,每次操作可以将任意一行或一列反转,即这一行或一列中0变为1,1变为0.问通过任意多次这样的变换,最多可以 ...

  4. 【POJ - 2078】Matrix(dfs)

    -->Matrix Descriptions: 输入一个n×n的矩阵,可以对矩阵的每行进行任意次的循环右移操作,行的每一次右移后,计算矩阵中每一列的和的最大值,输出这些最大值中的最小值. Sam ...

  5. 【UOJ#311】【UNR #2】积劳成疾(动态规划)

    [UOJ#311][UNR #2]积劳成疾(动态规划) UOJ Solution 考虑最大值分治解决问题.每次枚举最大值所在的位置,强制不能跨过最大值,左右此时不会影响,可以分开考虑. 那么设\(f[ ...

  6. 【UOJ#246】套路(动态规划)

    [UOJ#246]套路(动态规划) 题面 UOJ 题解 假如答案的选择的区间长度很小,我们可以做一个暴力\(dp\)计算\(s(l,r)\),即\(s(l,r)=min(s(l+1,r),s(l,r- ...

  7. 【LOJ#6074】子序列(动态规划)

    [LOJ#6074]子序列(动态规划) 题面 LOJ 题解 考虑一个暴力\(dp\). 设\(f[i][c]\)表示当前在第\(i\)位,并且以\(c\)结尾的子序列个数. 那么假设当前位为\(a\) ...

  8. 【POJ 3071】 Football(DP)

    [POJ 3071] Football(DP) Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4350   Accepted ...

  9. 通俗地说逻辑回归【Logistic regression】算法(二)sklearn逻辑回归实战

    前情提要: 通俗地说逻辑回归[Logistic regression]算法(一) 逻辑回归模型原理介绍 上一篇主要介绍了逻辑回归中,相对理论化的知识,这次主要是对上篇做一点点补充,以及介绍sklear ...

随机推荐

  1. UVAlive 3026 KMP 最小循环节

    KMP算法: 一:next数组:next[i]就是前面长度为i的字符串前缀和后缀相等的最大长度,也即索引为i的字符失配时的前缀函数. 二:KMP模板 /* pku3461(Oulipo), hdu17 ...

  2. Teamviewer ubuntu 提示 TeamViewer Daemon is not running

    http://blog.csdn.net/laohuang1122/article/details/12657343 Ubunut 12.04下面安装了Teamviewer,刚安装完启动是没有问题的, ...

  3. centos7备份还原与grub2引导和rescue模式修改root密码

    一.centos7备份1.su -2.cd /3.tar -zpPcvf backup.tgz --exclude=/sys --exclude=/mnt --exclude=/proc --excl ...

  4. [Angular] Modify User Provided UI with Angular Content Directives

    If we’re going to make our toggle accessible, we’ll need to apply certain aria attributes to the con ...

  5. 汉诺塔 Tower of Hanoi

    假设柱子标为A,B.C.要由A搬至C,在仅仅有一个盘子时,就将它直接搬至C:当有两个盘子,就将B作为辅助柱.假设盘数超过2个.将第二个下面的盘子遮起来,就非常easy了.每次处理两个盘子,也就是:A- ...

  6. 区分Integer.getInteger和Integer.valueOf、Integer.parseInt() 的使用方法

    Integer类有两个看起来很类似的静态方法,一个是Integer.getInteger(String),另外一个是Integer.valueOf(String).如果只看方法名称的话,很容易将这两个 ...

  7. Idea 13 新建maven项目

    1.此时生成的maven项目没有web文件夹 file→New Project→Maven→Next→GID.AID (NewDemo)→Next→ProjectName(NewDemo)→Finis ...

  8. 基于github for windows&amp;github的团队协作基本操作

    首先,我们要在github上团队协作.先要建立一个team.这个自行百度,在github上操作就是. 点击打开链接 这是我的有道文章(假设看不到图片的话) 今天主要讲的是怎么操作github for ...

  9. 目标跟踪之高斯混合模型---cv实现

    #include <stdio.h>#include <cv.h>#include <cxcore.h>#include <highgui.h>#inc ...

  10. mysql字段去重方式

    一直找不出某个字段去重的前提下,还能够显示其它字段的数据 以下是解决方法: SELECT *, COUNT(DISTINCT( province)) FROM area_info WHERE type ...