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. [vijos1891]学姐的逛街计划

                                                                     学姐的逛街计划 描述 doc 最近太忙了, 每天都有课. 这不怕, d ...

  2. 【.Net Core 学习系列】-- 自定义错误页面在IE浏览器中不能正常显示

    测试场景: 1. 新建.Net Core Web项目 2. 选择模板: 3. 修改Error页面代码:(去掉母版页并修改页面显示信息) 4. 修改[ASPNETCORE_ENVIRONMENT],并抛 ...

  3. Go --- 设计模式(工厂模式)

    简易工厂主要是用来解决对象“创建”的问题.以下的例子取自<大话设计模式>中第一章,实现一个可扩展的“计算器”.当增加新的功能时,并不需改动原来已经实现的算法.由于是简易工厂,所以我们还是需 ...

  4. java 返回json数据

    Student st1 = new Student(1, "dg", 18, new Date());            Student st2 = new Student(2 ...

  5. Ubuntu系统U盘安装以及降内核

    由于项目需要,要用U盘制作一个Linux系统,支持EFI启动,并且内核版本要求是2.6.35.6.所以在选系统的时候,就必须要选安装文件里面带有EF I目录,并且该目录下面有BOOTx64.EFI和g ...

  6. ldd

    ldd命令用于判断某个可执行的 binary 档案含有什么动态函式库 [diego@localhost ~/work/branch_dispatch_201511/rtqa_center/source ...

  7. Selenium系列之--01 简介【转】

    1.selenium 工具组件 1.1 selenium2,也称为selenium webdriver.webdriver原来是另一个自动化测试工具,后与selenium 合并了.webdriver直 ...

  8. 【教程】怎样申请Chrome应用商店(Web Store)开发人员

    首先你须要一张信用卡,假设你没有的话.能够借用父母或他人的(多见于学生党) 假设你有信用卡.你还得看看信用卡正面是否有注明"VISA"."MasterCard" ...

  9. iOS视频流开发(2) — 视频播放

    iOS视频流开发(2) — 视频播放  承上篇,本篇文章主要介绍iOS视频播放需要用到的类.以及他们的使用场景和开发中遇到的问题. MPMoviePlayerViewController MP简介 i ...

  10. net start sshd 发生系统错误1069--cygwin安装过程

    net start sshd 发生系统错误1069 解决方法: services.msc调出服务,然后CYGWIN sshd服务->属性,修改账户的名字和密码(win7的登录名和密码) 可能还遇 ...