HDU 1501 Zipper(DP,DFS)
意甲冠军 是否可以由串来推断a,b字符不改变其相对为了获取字符串的组合c
本题有两种解法 DP或者DFS
考虑DP 令d[i][j]表示是否能有a的前i个字符和b的前j个字符组合得到c的前i+j个字符 值为0或者1 那么有d[i][j]=(d[i-1][j]&&a[i]==c[i+j])||(d[i][j-1]&&b[i]==c[i+j]) a,b的下标都是从1開始的 注意0的初始化
#include<cstdio>
#include<cstring>
using namespace std;
const int N = 205;
char a[N], b[N], c[2 * N];
bool d[N][N]; int main()
{
int cas;
scanf ("%d", &cas);
for (int k = 1; k <= cas; ++k)
{
scanf ("%s%s%s", a + 1, b + 1, c + 1);
int la = strlen (a + 1), lb = strlen (b + 1), i = 1, j = 1;
memset (d, 0, sizeof (d)); while (a[i] == c[i] && i <= la)
d[i++][0] = true;
while (b[j] == c[j] && j <= lb)
d[0][j++] = true;
for (int i = 1; i <= la; ++i)
for (int j = 1; j <= lb; ++j)
d[i][j] = ( (d[i - 1][j] && a[i] == c[i + j]) || (d[i][j - 1] && b[j] == c[i + j])); printf ("Data set %d: ", k);
printf (d[la][lb] ? "yes\n" : "no\n");
}
return 0;
}
以下是dfs的代码 看是否能在ab中相应搜到c的每个字母就可
//DFS版
#include <cstdio>
#include <cstring>
using namespace std;
const int N = 205;
char a[N], b[N], c[2 * N];
bool vis[N][N], ans;
void dfs (int i, int j, int k)
{
if (c[k] == '\0') ans = true;
if (ans || vis[i][j]) return ;
vis[i][j] = true;
if (a[i] == c[k]) dfs (i + 1, j, k + 1);
if (b[j] == c[k]) dfs (i, j + 1, k + 1);
}
int main()
{
int cas;
scanf ("%d", &cas);
for (int ca = 1; ca <= cas; ++ca)
{
ans = false;
memset (vis, 0, sizeof (vis));
scanf ("%s%s%s", a, b, c);
dfs (0, 0, 0);
printf ("Data set %d: ", ca);
printf (ans ? "yes\n" : "no\n");
}
return 0;
}
Zipper
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".
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.
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.
3
cat tree tcraete
cat tree catrtee
cat tree cttaree
Data set 1: yes
Data set 2: yes
Data set 3: no
版权声明:本文博主原创文章,博客,未经同意不得转载。
HDU 1501 Zipper(DP,DFS)的更多相关文章
- HDU 1501 Zipper 【DFS+剪枝】
HDU 1501 Zipper [DFS+剪枝] Problem Description Given three strings, you are to determine whether the t ...
- hdu 1501 Zipper dfs
题目链接: HDU - 1501 Given three strings, you are to determine whether the third string can be formed by ...
- (step4.3.5)hdu 1501(Zipper——DFS)
题目大意:个字符串.此题是个非常经典的dfs题. 解题思路:DFS 代码如下:有详细的注释 /* * 1501_2.cpp * * Created on: 2013年8月17日 * Author: A ...
- hdu 1501 Zipper(DP)
题意: 给三个字符串str1.str2.str3 问str1和str2能否拼接成str3.(拼接的意思可以互相穿插) 能输出YES否则输出NO. 思路: 如果str3是由str1和str2拼接而成,s ...
- HDU 1501 Zipper(DFS)
Problem Description Given three strings, you are to determine whether the third string can be formed ...
- hdu 1501 Zipper
链接:http://acm.hdu.edu.cn/showproblem.php?pid=1501 思路:题目要求第三个串由前两个组成,且顺序不能够打乱,搜索大法好 #include<cstdi ...
- HDU 1087 简单dp,求递增子序列使和最大
Super Jumping! Jumping! Jumping! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 ...
- HDU(1572),最短路,DFS
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1572 很久没写深搜了,有点忘了. #include <iostream> #include ...
- 140. Word Break II (String; DP,DFS)
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...
随机推荐
- 【Android进阶】判断网络连接状态并自动界面跳转
用于判断软件打开时的网络连接状态,若无网络连接,提醒用户跳转到设置界面 /** * 设置在onStart()方法里面,可以在界面每次获得焦点的时候都进行检测 */ @Override protecte ...
- LeetCode——ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- c# 判断字符是否是全角, 获取字符串的字节数 , 获取字符串指定长度字节数的字符串
1 Encoding.Default.GetByteCount(checkString); =2 全角 =1 半角 /// <summary> /// 获取字符串的字节长度 /// &l ...
- SQLSERVER存储过程语法的具体解释
SQL SERVER存储过程语法: Create PROC [ EDURE ] procedure_name [ ; number ] [ { @parameter data_type } ...
- StackExchange.Redis 使用
StackExchange.Redis 使用 - 事件(五) 摘要: ConnectionMultiplexer 可以注册如下事件ConfigurationChanged- 配置更改时Configur ...
- 系列五AnkhSvn
原文:系列五AnkhSvn AnkhSvn介绍 AnkhSVN是一款在VS中管理Subversion的插件,您可以在VS中轻松的提交.更新.添加文件,而不用在命令行或资源管理器中提交.而且该插件属于开 ...
- HDFS Safemode问题
处于safemode的集群是无法接收不论什么写操作的,包含创建文件夹.删除文件.改动文件.上传文件等等. 关于safemode,在http://www.iteblog.com/archives/977 ...
- MysqL的root用户不允许远程连接
原文:MysqL的root用户不允许远程连接 今天程序报了异常:java.sql.SQLException: Access denied for user 'root'@'RJB-Z' (using ...
- Android使用SVG矢量创建很酷的动态效率!
尊重原创,欢迎转载.转载请注明: FROM GA_studio http://blog.csdn.net/tianjian4592 一个真正酷炫的动效往往让人虎躯一震,话不多说.咱们先瞅瞅效果: ...
- mybatis generator插件开发
mybatis现在普遍使用的每一个人DAO框架.mybatis generator它可以基于数据库中的表结构,生成自己主动mybatis代码和配置文件,方便使用,当然,实际的使用过程中.generat ...