1010. Zipper

Constraints

Time Limit: 1 secs, Memory Limit: 32 MB

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

动态规划,开一个二维数组,dp[i][j] 表示 以s0s1s2.....s(i-1)与t0t1t2...t(j-1)构成c0c1...c(i+j-1)的可能性。

基态:

if(s[i] == c[i] ) dp[i+1][0] = 1;

if(t[j] == c[j]) dp[0][j+1] = 1;

递归态:

if(dp[i-1][j] && s[i-1] == c[i+j-1]) dp[i][j] = 1;

if(dp[i][j-1] && t[j-1] == c[i+j-1]) dp[i][j] = 1;

代码如下:

#include <iostream>
#include <string>
#include <memory.h>
using namespace std; int dp[202][202]; int main()
{
int N;
cin >> N;
int count = 0;
while(N--)
{
count++;
memset(dp,0,sizeof(dp));
string ss,tt,cc;
cin >> ss >> tt >> cc;
int i,j;
for(i = 0;i < ss.size();i++)
{
if(ss[i] == cc[i])
dp[i+1][0] = 1;
}
for(j = 0;j < tt.size();j++)
{
if(tt[j] == cc[j])
dp[0][j+1] = 1;
}
for(i = 1;i <= ss.size();i++)
{
for(j = 1;j <= tt.size();j++)
{
if(dp[i-1][j] && ss[i-1] == cc[i+j-1])
dp[i][j] = 1;
if(dp[i][j-1] && tt[j-1] == cc[i+j-1])
dp[i][j] = 1;
}
}
int len1 = ss.size();
int len2 = tt.size();
if(dp[len1][len2])
cout << "Data set " << count << ": " << "yes" << endl;
else
cout << "Data set " << count << ": " << "no" << endl;
}
return 0;
}

soj1010. Zipper的更多相关文章

  1. POJ 2192 :Zipper(DP)

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

  2. Zipper

      Zipper Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

  3. HDU 1501 Zipper 动态规划经典

    Zipper Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Sub ...

  4. HDU 1501 Zipper(DP,DFS)

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

  5. hdu1501 Zipper

    Zipper Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submis ...

  6. Zipper(poj2192)dfs+剪枝

    Zipper Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15277   Accepted: 5393 Descripti ...

  7. Haskell语言学习笔记(36)Data.List.Zipper

    ListZipper 模块 $ cabal install ListZipper Installed ListZipper-1.2.0.2 Prelude> :m +Data.List.Zipp ...

  8. HDU1501 Zipper(DFS) 2016-07-24 15:04 65人阅读 评论(0) 收藏

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

  9. HDU 1501 Zipper 【DFS+剪枝】

    HDU 1501 Zipper [DFS+剪枝] Problem Description Given three strings, you are to determine whether the t ...

随机推荐

  1. 第七周PSP&进度条

    团队项目PSP 一.表格:     C类型 C内容 S开始时间 E结束时间 I时间间隔 T净时间(mins) 预计花费时间(mins) 讨论 讨论beta阶段任务 10:00 12:30 28 270 ...

  2. cxGrid 单元格回车移到下一行,当移到最后一个单元格时回车新增一行【转】

    1 在TcxGridDBTableView中,设定属性 NewItemRow.Visible = True 2 在cxgrid中输入数据怎样回车换行  在TcxGridDBTableView中  将属 ...

  3. windows多线程(十) 生产者与消费者问题

    一.概述 生产者消费者问题是一个著名的线程同步问题,该问题描述如下:有一个生产者在生产产品,这些产品将提供给若干个消费者去消费,为了使生产者和消费者能并发执行,在两者之间设置一个具有多个缓冲区的缓冲池 ...

  4. [转帖] JVM虚拟机的历史

    Java虚拟机发展史 https://blog.csdn.net/tinyDolphin/article/details/72809018 如何查看自己的虚拟机版本?Sun Classic / Exa ...

  5. 官方下拉刷新控件SwipeRefreshLayout的使用

    今天看博客,发现有了这个下拉刷新的控件,效果看上去还蛮好的,于是我也想研究的是使用一下,写个demo.其实使用很简单的,但就是为了能使用这个新组建我下了好久的更新,后来还是直接去官网下载最新的ADT得 ...

  6. spring 整合 Struts1.X [转]

    这篇博客摘自[http://blog.csdn.net/chendc201/article/details/8464008], 其中也有一些是自己增加的部分 . 第一步, 需要为 Struts 装载 ...

  7. JVM内存模型二

    Java 中通过多线程机制使得多个任务同时执行处理,所有的线程共享JVM内存区域main memory,而每个线程又单独的有自己的工作内存,当线程与内存区域进行交互时,数据从主存拷贝到工作内存,进而交 ...

  8. Windows安装ElastAlert问题总结

    1.运行时出现UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0xb4 in position 0: invalid start byte 或  ...

  9. A Mist of Florescence CodeForces - 989C(思维构造)

    题意: 让你构造一个图,使得A,B,C,D的个数为给定的个数,上下左右连通的算一个. 哎呀 看看代码就懂了..emm..很好懂的 #include <bits/stdc++.h> usin ...

  10. 【BZOJ2463】谁能赢呢?(博弈论)

    [BZOJ2463]谁能赢呢?(博弈论) 题面 BZOJ 洛谷 题解 洛谷上对于难度的评级我总觉有些问题. 很多人按照代码难度而并非思维难度在评级,导致很多评级很不合理啊... 不说废话了.. 对于一 ...