Zipper

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". 

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)的更多相关文章

  1. HDU 1501 Zipper(DP,DFS)

    意甲冠军  是否可以由串来推断a,b字符不改变其相对为了获取字符串的组合c 本题有两种解法  DP或者DFS 考虑DP  令d[i][j]表示是否能有a的前i个字符和b的前j个字符组合得到c的前i+j ...

  2. HDU 1501 & POJ 2192 Zipper(dp记忆化搜索)

    题意:给定三个串,问c串是否能由a,b串任意组合在一起组成,但注意a,b串任意组合需要保证a,b原串的顺序 例如ab,cd可组成acbd,但不能组成adcb. 分析:对字符串上的dp还是不敏感啊,虽然 ...

  3. POJ 2192 :Zipper(DP)

    http://poj.org/problem?id=2192 Zipper Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1 ...

  4. HDOJ 1501 Zipper 【简单DP】

    HDOJ 1501 Zipper [简单DP] Problem Description Given three strings, you are to determine whether the th ...

  5. HDOJ 1501 Zipper 【DP】【DFS+剪枝】

    HDOJ 1501 Zipper [DP][DFS+剪枝] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Ja ...

  6. POJ2192 - Zipper(区间DP)

    题目大意 给定三个字符串s1,s2,s3,判断由s1和s2的字符能否组成字符串s3,并且要求组合后的字符串必须是s1,s2中原来的顺序. 题解 用dp[i][j]表示s1的前i个字符和s2的前j个字符 ...

  7. poj 2192 Zipper(区间dp)

    题目链接:http://poj.org/problem?id=2192 思路分析:该问题可以看做dp问题,同时也可以使用dfs搜索求解,这里使用dp解法: 设字符串StrA[0, 1, …, n]和S ...

  8. HUD 1501 Zipper(记忆化 or DP)

    Problem Description Given three strings, you are to determine whether the third string can be formed ...

  9. ZOJ2401 Zipper 双塔式 DP

    遇到双塔DP,写一下. flag是为了避免memset多次导致的时间浪费. #include<cstdio> #include<cstdlib> #include<ios ...

随机推荐

  1. j2ee、mvn、eclipse、Tomcat等中文乱码问题解决方法

    一.更改jdk默认编码为UTF-8,保证启动的JVM不会出现中文乱码问题 1.在编译的时候,如果我们没有用 -encoding 参数指定我们的JAVA源程序的编码格式,则javac.exe首先获得我们 ...

  2. python实现将文件夹内所有txt文件合并成一个文件

    新建一个文件夹命名为yuliao,把所有txt文件放进去就ok啦!注意路径中‘/’,windows下路径不是这样. #coding=utf-8  import os #获取目标文件夹的路径 filed ...

  3. Nginx常用Rewrite(伪静态规则)WordPress/PHPCMS/ECSHOP/ShopEX/SaBlog/Discuz/DiscuzX/PHPWind/Typecho/DEDECMS

    目前已收集Wordpress.Wordpress二级目录.PHPCMS.ECSHOP.ShopEX.SaBlog.Discuz.Discuz X.PHPWind.Typecho.DEDECMS: Wo ...

  4. P6 EPPM 16 R1安装和配置文档

    白桃花心木P6企业项目组合管理文档库  描述 链接 下载 零件号  16 R1用户和集成文档 查看库 下载 E68199-01 16 R1安装和配置文档 查看库 下载 E68198-01 描述 链接 ...

  5. Node.js静态页面展示例子2

    例程下载:https://files.cnblogs.com/files/xiandedanteng/nodejsStaticHtmlSample.rar 页面效果: Html页面代码(注意用文本编辑 ...

  6. java.lang.IllegalArgumentException: Wrong state classs

    java.lang.IllegalArgumentException: Wrong state class, expecting View State but received class cn.et ...

  7. 遍历修改django bootstrap form 为 django bootstrap3

    #!/usr/bin/env python # encoding: utf-8 import re import os fname = '' bt_pattern = re.compile(r'{% ...

  8. 国内外DNS服务器地址列表大全

    DNS(Domain Name System)是域名解析服务器的意思,它在互联网的作用是把域名转换成为网络可以识别的IP地址. 通常来说,香港.韩国.日本等国的DNS服务器速度会比较快,大家可以多用几 ...

  9. 捕获php请求内容

    通过将phpinfo()打印到文件实现 <?php echo str_pad('', 1024);//使缓冲区溢出 ob_start();//打开缓冲区 phpinfo(); $string = ...

  10. go结构体组合函数

    结构体定义 上面我们说过Go的指针和C的不同,结构体也是一样的.Go是一门删繁就简的语言,一切令人困惑的特性都必须去掉. 简单来讲,Go提供的结构体就是把使用各种数据类型定义的不同变量组合起来的高级数 ...