【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 ...
随机推荐
- java 数组 输入5名学生的成绩 得出平均分。
import java.util.Scanner; public class LianXi4{ public static void main(String[] args){ //创建长度为5的数组 ...
- 转:TLV 格式及编解码示例
TLV是一种可变格式,意思就是: Type类型, Lenght长度,Value值: Type和Length的长度固定,一般那是2.4个字节(这里统一采用4个字节): Value的长度有Length指定 ...
- freeswitch对媒体的处理的三种方式
一.默认方式:媒体通过freeswitch, RTP被freeswtich转发, freeswitch控制编码的协商并在协商不一致时提供语音编码转换能力, 支持录音,二次拨号等. 二.代理模式: ...
- SpringBoot初始教程之日志处理(二)
SpringBoot初始教程之日志处理(二) 1.介绍 SpringBoot默认是采用logback进行日志处理.Logback是由log4j创始人设计的又一个开源日志组件.Logback是由log4 ...
- md5sum使用注意事项
1. linux 命令行的 md5sum命令 echo -n "123456" | md5sum //echo的时候, 默认是自带回车的, 必须要去掉 -n 去掉回车. 2 ...
- iOS开发--URL中汉字出现乱码
NSURL *nurl=[[NSURL alloc] initWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF ...
- js中window.onload 与 jquery中$(document.ready()) 測试
js中window.onload 与 jquery中$(document.ready())差别,验证代码例如以下(调换js代码和Jquer代码书写顺序測试.执行结果一样.因此与代码书写位置没关系): ...
- Jenkins系列之-—05 节点配置
一.节点配置 1. 进入[系统管理]-[节点管理]-[新建节点],录入节点名,选择Permanent Agent,下一步录入节点详细配置信息,如下: Name:节点名称 Description:节点描 ...
- sqlserver中All、Any和Some用法与区别
转自:http://blog.csdn.net/gyc1105/article/details/8063624 SQLServer中有三个关键字可以修改比较运算符:All.Any和Some,其中Som ...
- 正向代理tinyproxy使用总结
使用tinyproxy的问题背景: 其实以前代理一直用的是apache,后来,那次有个任务要给ios的推送设置代理,任务很紧急,可是apache报错. 原因如下:APNS发送通知的端口2195,但是A ...