Zipper (DP)
Zipper
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".
InputThe 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.
OutputFor 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 题意:给出 A,B,C 三个字符串,问 A 不改变顺序的插入 B 中能否得到 C
因为长为 i 的字符串 A 和长为 j 的字符串 B 要能组成 C ,C的最后一个必定属于 A 或者 B
dp [i][j] 意为 dp[i][j] 意为 A的前i位,B的前j位能否组成C的前i+j位
dp[i][j] = (dp[i-1][j]&&A[i-1]==C[i+j-1])||(dp[i][j-1]&&B[j-1]==C[i+j-1]);
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
#define MX 205
char A[MX],B[MX],C[MX*];
int dp[MX][MX]; //dp[i][j] 意为 A的前i位,B的前j位能否组成C的前i+j位 int main()
{
int T;
cin>>T;
for (int cnt=;cnt<=T;cnt++)
{
scanf("%s %s %s",A,B,C);
int lena = strlen(A);
int lenb = strlen(B);
memset(dp,,sizeof(dp));
dp[][]=;
for (int i=;i<=lena;i++)
if (A[i-]==C[i-]&&dp[i-][])
dp[i][]=;
for (int i=;i<=lenb;i++)
if (B[i-]==C[i-]&&dp[][i-])
dp[][i]=;
for (int i=;i<=lena;i++)
for (int j=;j<=lenb;j++)
dp[i][j] = (dp[i-][j]&&A[i-]==C[i+j-])||(dp[i][j-]&&B[j-]==C[i+j-]);
printf("Data set %d: ",cnt);
if (dp[lena][lenb])
printf("yes\n");
else
printf("no\n");
}
return ;
}
Zipper (DP)的更多相关文章
- HDU 1501 Zipper(DP,DFS)
意甲冠军 是否可以由串来推断a,b字符不改变其相对为了获取字符串的组合c 本题有两种解法 DP或者DFS 考虑DP 令d[i][j]表示是否能有a的前i个字符和b的前j个字符组合得到c的前i+j ...
- HDU 1501 & POJ 2192 Zipper(dp记忆化搜索)
题意:给定三个串,问c串是否能由a,b串任意组合在一起组成,但注意a,b串任意组合需要保证a,b原串的顺序 例如ab,cd可组成acbd,但不能组成adcb. 分析:对字符串上的dp还是不敏感啊,虽然 ...
- POJ 2192 :Zipper(DP)
http://poj.org/problem?id=2192 Zipper Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1 ...
- HDOJ 1501 Zipper 【简单DP】
HDOJ 1501 Zipper [简单DP] Problem Description Given three strings, you are to determine whether the th ...
- HDOJ 1501 Zipper 【DP】【DFS+剪枝】
HDOJ 1501 Zipper [DP][DFS+剪枝] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Ja ...
- POJ2192 - Zipper(区间DP)
题目大意 给定三个字符串s1,s2,s3,判断由s1和s2的字符能否组成字符串s3,并且要求组合后的字符串必须是s1,s2中原来的顺序. 题解 用dp[i][j]表示s1的前i个字符和s2的前j个字符 ...
- poj 2192 Zipper(区间dp)
题目链接:http://poj.org/problem?id=2192 思路分析:该问题可以看做dp问题,同时也可以使用dfs搜索求解,这里使用dp解法: 设字符串StrA[0, 1, …, n]和S ...
- HUD 1501 Zipper(记忆化 or DP)
Problem Description Given three strings, you are to determine whether the third string can be formed ...
- ZOJ2401 Zipper 双塔式 DP
遇到双塔DP,写一下. flag是为了避免memset多次导致的时间浪费. #include<cstdio> #include<cstdlib> #include<ios ...
随机推荐
- QT5.8+vs2015配置以及qt creater中出现中文乱码解决办法之一
1.参考此文档:QT5.6+vs2015配置: 2.出现乱码问题时候 在头文件上加入: #pragma execution_character_set("utf-8") //加入这 ...
- ASP.NET MVC学习---(二)EF文件结构
之前已经简单的介绍过ORM框架和EF 也了解了EF的种种优点 那么这个EF到底长啥样子都还没见过呢 别着急 接下来,科学教育频道--走近科学 带你走进EF的内心世界~ 那么接下来就是~ 等等等等... ...
- EffectiveJava(10)覆盖equals是视情况覆盖toString
覆盖equals是视情况覆盖toString 1.toString返回字符串 className@163b91 -calssName 类的名称 @ @ 163b91 散列码的无符号十六进制表示法 2. ...
- 转载:JS进度条
转载地址:http://blog.csdn.net/treeClimber/article/details/569974 代码在原基础上稍作改动,如下: <!DOCTYPE HTML PUBLI ...
- Win7 Visual Studio 2008如何注册
默认是90天试用 在控制面板中卸载程序,然后找到VS2008,点击卸载/更改 到这一步就不要动了 去下载并运行CrackVS2008ForWindows7,然后点击右上角的Bug微软,弹出 ...
- eclipse黄色警告(finally block does not complete normally) ,不建议在finally中使用return语句
在eclipse中编写例如以下的代码,eclipse会给出黄色告警:finally block does not complete normally. public class Test { publ ...
- 手写 jQuery 框架
1.测试页面; <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...
- react-native 常见问题 及 解决方案
一.报错 Warning:Navigator:isMounted is deprecated. Instead, make sure to clean up subscriptions and pen ...
- python常见面试题(一)
1.Python是如何进行内存管理的? 答:从三个方面来说,一对象的引用计数机制,二垃圾回收机制,三内存池机制 一.对象的引用计数机制 Python内部使用引用计数,来保持追踪内存中的对象,所有对象都 ...
- Newtonsoft.Json之JArray, JObject, JProperty,JValue
JObject staff = new JObject(); staff.Add(new JProperty("Name", "Jack")); staff.A ...