博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 1501 Zipper
阅读量:5276 次
发布时间:2019-06-14

本文共 3078 字,大约阅读时间需要 10 分钟。

题目链接:

Description

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

题意

给定3个字符串,问在不改变前两个字符串内字母的相对位置的前提下能否组成第3个字符串,保证前两个字符串长度之和等于第三个字符串。

题解:

这道题的解法有多种,有说用DP的,有说用最短路的、、、无奈我只会DFS,最后用DFS跑出来的这道题,而且思路也比较简单。

设置一个标记,表示匹配到了第3个字符串的第几个字母,在DFS时,只要有一个字母被跳过了,那就说明肯定无解,因为:保证前两个字符串长度之和等于第三个字符串,在输入时加了一个对字母组成的剪枝,刚开始超时就卡在这个判断上了。

代码

#define _CRT_SECURE_NO_WARNINGS#include
#include
#include
using namespace std;typedef long long ll;char a[210], b[210], g[450];int cur1, cur2;bool dfs(int temp) { if (temp == strlen(g)) { return true; } if (a[cur1] == g[temp]) { cur1++; if(dfs(temp + 1)) return true; cur1--; } if (b[cur2] == g[temp]) { cur2++; if(dfs(temp + 1)) return true; cur2--; } return false;}int main() { int Case = 1; int t; scanf("%d", &t); map
P1; map
P2; while (t--) { P1.clear(); P2.clear(); cur1 = cur2 = 0; scanf("%s%s%s", a, b, g); for (int i(0); i < strlen(a); i++) P1[a[i]]++; for (int i(0); i < strlen(b); i++) P1[b[i]]++; for (int i(0); i < strlen(g); i++) P2[g[i]]++; if (P1 != P2) { printf("Data set %d: no\n", Case++); } else { if (!dfs(0)) { printf("Data set %d: no\n", Case++); } else printf("Data set %d: yes\n", Case++); } } return 0;}

转载于:https://www.cnblogs.com/Titordong/p/9593942.html

你可能感兴趣的文章
tensorflow saver简介+Demo with linear-model
查看>>
Luogu_4103 [HEOI2014]大工程
查看>>
Oracle——SQL基础
查看>>
项目置顶随笔
查看>>
Redis的安装与使用
查看>>
P1970 花匠
查看>>
java语言与java技术
查看>>
NOIP2016提高A组五校联考2总结
查看>>
iOS 项目的编译速度提高
查看>>
table中checkbox选择多行
查看>>
Magento开发文档(三):Magento控制器
查看>>
性能调优攻略
查看>>
ie6解决png图片透明问题
查看>>
瞬间的永恒
查看>>
2019-8-5 考试总结
查看>>
JS中实现字符串和数组的相互转化
查看>>
web service和ejb的区别
查看>>
Windows Azure Cloud Service (29) 在Windows Azure发送邮件(下)
查看>>
CS61A Efficiency 笔记
查看>>
微信上传素材返回 '{"errcode":41005,"errmsg":"media data missing"}',php5.6返回
查看>>