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. iOS开发值得收藏的博客

    http://kobedai.me/ objc.io PS:经典,内容深而广objc中国NSHipster PS:很多小细节NSHipster 中文版唐巧的技术博客 PS:LZ是唐巧的脑残粉…OneV ...

  2. PHP 更改session存储方式为Redis

    前言: 服务器默认的session存放方式是file.当客户端发送请求带有PHPSESSID时是顺序的去比对session存储文件,如果有5000个session文件,那就有可能需要比对4998次那么 ...

  3. HDU 1231 最大子序列

    http://acm.hdu.edu.cn/showproblem.php?pid=1231 Problem Description 给定K个整数的序列{ N1, N2, ..., NK },其任意连 ...

  4. JS单例模式在工作中的使用

    为了尽可能的减少全局变量的污染,在写js的时候可以采用单例模式,形式如下: 比如有一个js叫demo.js,那么我们可以在js里这样写: var demo = {} 这样做的目的是将整个js当成一个对 ...

  5. virsh 操作kvm虚拟机

    #查看你的硬件是否支持虚拟化.命令: [root@VM_166_143 data]#egrep '(vmx|svm)' /proc/cpuinfo #安装基础包 [root@VM_166_143 da ...

  6. 【Java线程】SwingWorker的用法

    Swing应用程序员常见的错误是误用Swing事件调度线程(Event DispatchThread,EDT).他们要么从非UI线程访问UI组件:要么不考虑事件执行顺序:要么不使用独立任务线程而在ED ...

  7. Ubuntu 12.04下LAMP环境搭建实录

    方法一: 用tasksel可以方便安装dns server, lamp, kubuntu desktop, ubuntu desktop, xubuntu之类的软件包.这个软件在ubuntu serv ...

  8. BZOJ 2521: [Shoi2010]最小生成树

    2521: [Shoi2010]最小生成树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 445  Solved: 262[Submit][Statu ...

  9. Climbing Stairs - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Climbing Stairs - LeetCode 注意点 注意边界条件 解法 解法一:这道题是一题非常经典的DP题(拥有非常明显的重叠子结构).爬到n ...

  10. IO编程(2)-操作文件和目录

    操作文件和目录 如果我们要操作文件.目录,可以在命令行下面输入操作系统提供的各种命令来完成.比如dir.cp等命令. 如果要在Python程序中执行这些目录和文件的操作怎么办?其实操作系统提供的命令只 ...