【OpenJ_Bailian - 2192】Zipper(dfs)
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)的更多相关文章
- 【POJ - 1950】Dessert(dfs)
-->Dessert Descriptions: 给你一个数N(3<=N<=15);每个数之间有三种运算符“‘+’,‘-’,‘.’”.输出和值等于零的所有的运算情况及次数num,如果 ...
- 【OpenJ_Bailian - 2795】金银岛(贪心)
金银岛 Descriptions: 某天KID利用飞行器飞到了一个金银岛上,上面有许多珍贵的金属,KID虽然更喜欢各种宝石的艺术品,可是也不拒绝这样珍贵的金属.但是他只带着一个口袋,口袋至多只能装重量 ...
- 【Aizu - 0525】Osenbei (dfs)
-->Osenbei 直接写中文了 Descriptions: 给出n行m列的0.1矩阵,每次操作可以将任意一行或一列反转,即这一行或一列中0变为1,1变为0.问通过任意多次这样的变换,最多可以 ...
- 【POJ - 2078】Matrix(dfs)
-->Matrix Descriptions: 输入一个n×n的矩阵,可以对矩阵的每行进行任意次的循环右移操作,行的每一次右移后,计算矩阵中每一列的和的最大值,输出这些最大值中的最小值. Sam ...
- 【UOJ#311】【UNR #2】积劳成疾(动态规划)
[UOJ#311][UNR #2]积劳成疾(动态规划) UOJ Solution 考虑最大值分治解决问题.每次枚举最大值所在的位置,强制不能跨过最大值,左右此时不会影响,可以分开考虑. 那么设\(f[ ...
- 【UOJ#246】套路(动态规划)
[UOJ#246]套路(动态规划) 题面 UOJ 题解 假如答案的选择的区间长度很小,我们可以做一个暴力\(dp\)计算\(s(l,r)\),即\(s(l,r)=min(s(l+1,r),s(l,r- ...
- 【LOJ#6074】子序列(动态规划)
[LOJ#6074]子序列(动态规划) 题面 LOJ 题解 考虑一个暴力\(dp\). 设\(f[i][c]\)表示当前在第\(i\)位,并且以\(c\)结尾的子序列个数. 那么假设当前位为\(a\) ...
- 【POJ 3071】 Football(DP)
[POJ 3071] Football(DP) Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4350 Accepted ...
- 通俗地说逻辑回归【Logistic regression】算法(二)sklearn逻辑回归实战
前情提要: 通俗地说逻辑回归[Logistic regression]算法(一) 逻辑回归模型原理介绍 上一篇主要介绍了逻辑回归中,相对理论化的知识,这次主要是对上篇做一点点补充,以及介绍sklear ...
随机推荐
- Tomcat错误信息(服务器版本号)泄露(低危)
一.问题描述Tomcat报错页面泄漏Apache Tomcat/7.0.52相关版本号信息,是攻击者攻击的途径之一.因此实际当中建议去掉版本号信息. 二.解决办法 1.进入到tomcat/lib目录下 ...
- Shiro note
我们需要实现Realms的Authentication 和 Authorization.其中 Authentication 是用来验证用户身份,Authorization 是授权访问控制,用于对用户进 ...
- 202. Segment Tree Query
最后更新 二刷 09-Jan-17 正儿八经线段树的应用了. 查找区间内的值. 对于某一个Node,有这样的可能: 1)需要查找区间和当前NODE没有覆盖部分,那么直接回去就行了. 2)有覆盖的部分, ...
- POJ-2240 -Arbitrage(Bellman)
题目链接:Arbitrage 让这题坑了,精度损失的厉害.用赋值的话.直接所有变成0.00了,无奈下,我仅仅好往里输了,和POJ1860一样找正环,代码也差点儿相同,略微改改就能够了,可是这个题精度损 ...
- 西门子PLC学习笔记六-(Step7指令简单介绍)
1.指令操作数 指令操作数由操作标示符和參数组成. 操作标识符由主标识符和辅标识符组成. 主标识符有:I(输入过程影像寄存器).Q(输出过程映像寄存器).M(位寄存器).PI(外部输入寄存器).PQ( ...
- 答读者问(6):有关IT培训和毕业之前的迷茫等问题
近期在微博上与一些读者朋友们交流,发现大家对自己的未来都比較的关心.有些朋友认为在大学里面没有学到什么东西,问我要不要到一些IT培训机构去"速成".另一些朋友即将毕业,不知道自己走 ...
- 使用python生成c文件模板
目标 完成一个python脚本,实现指定名字后,自动生成.c和.h的模板.例如: /** * @file epc.c * @author 陈维 * @version V01 * @date 2017. ...
- 理解Paxos Made Practical
Paxos Made Practical 当一个组中一台机器提出一个值时,其它成员机器通过PAXOS算法在这个值上达成一致. Paxos分三个阶段. 第一阶段: 提出者会选出一个提议编号n(n> ...
- NoSQL之Redis探析
下载地址:wget http://download.redis.io/releases/redis-2.8.8.tar.gz安装steps:1 下载Official Website : http:// ...
- [IT学习]华为全连接大会2017
1.5分钟.3分钟.1分钟倒计时. 2.20万盏纽约街头的油灯接入电网,类比未来的公司IT系统会接入云? 3.1943年,全球只要5台计算机.不会的,但是会有5多云? 4.与航空业的联盟类比,云计算的 ...