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

【题意】给出三个字符串,求前两个是否包含在第三个中。

【思路】之前用过dfs,这次用dp,检验dp[len1][len2]是否为1;

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
const int N=;
int dp[N][N];
int main()
{
int n;
char a[N],b[N],c[N];
int cas=;
while(~scanf("%d",&n))
{
cas++;
memset( dp,,sizeof(dp));
scanf("%s%s%s",a+,b+,c+);
int len1=strlen(a+);
int len2=strlen(b+);
int len3=strlen(c+);
for(int i=;i<=len1;i++)
{
if(a[i]==c[i]) dp[i][]=;
else break;
}
for(int j=;j<=len2;j++)
{
if(b[j]==c[j])
dp[][j]=;
else break;
}
for(int i=;i<=len1;i++)
{
for(int j=;j<=len2;j++)
{
if(c[i+j]==a[i]&&dp[i-][j])
dp[i][j]=;
if(c[i+j]==b[j]&&dp[i][j-])
dp[i][j]=;
}
}
printf("Data set %d: ",cas);
if(dp[len1][len2]) printf("yes\n");
else printf("no\n");
}
return ;
}

Zipper_DP的更多相关文章

随机推荐

  1. 自定义jQuery插件Step by Step

    1.1.1 摘要 随着前端和后端技术的分离,各大互联网公司对于 Mobile First理念都是趋之若鹜的,为了解决网页在不同移动设备上的显示效果,其中一个解决方案就是Responsive Desig ...

  2. easyUI dialog 弹窗 居中显示

    默认情况下应该是在屏幕居中显示的.但是有的时候没有居中只要重新纠正下就可以了 $('#add_dialog').dialog('open'); //打开添加对话框 $('#add_dialog').w ...

  3. 创建交货单/外向交货BAPI_OUTB_DELIVERY_CREATE_SLS/STO

    FUNCTION Z_SD_CREATE_DN. *"-------------------------------------------------------------------- ...

  4. sequential minimal optimization,SMO for SVM, (MATLAB code)

    function model = SMOforSVM(X, y, C ) %sequential minimal optimization,SMO tol = 0.001; maxIters = 30 ...

  5. case when的用法

    国家(country)人口(population)           中国600            美国100            加拿大100            英国200       ...

  6. java 面向对象编程-- 第15章 集合框架

    1.  集合特点:元素类型不同.集合长度可变.空间不固定 2.  java中对一些数据结构和算法进行了封装即集合.集合也是一种对象,用于存储.检索.操作和传输对象. 3.  JCF(Java Coll ...

  7. nginx 配置优化的几个参数

    nginx 配置优化的几个参数 2011-04-22 本文地址: http://blog.phpbean.com/a.cn/7/ --水平有限欢迎指正-- -- 最近在服务器上搞了一些nginx 研究 ...

  8. S1 :闭包

    闭包是指有权访问另一个函数作用域中的变量的函数.创建闭包的常见方式,就是在一个函数内部创建另一个函数,以createComparisonFunction()函数为例 function createCo ...

  9. limit 百万级数据分页优化方法

    mysql教程 这个数据库教程绝对是适合dba级的高手去玩的,一般做一点1万 篇新闻的小型系统怎么写都可以,用xx框架可以实现快速开发.可是数据量到了10万,百万至千万,他的性能还能那么高吗? 一点小 ...

  10. LibSVM使用指南

    LibSVM使用指南 一.     SVM简介 在进行下面的内容时我们认为你已经具备了数据挖掘的基础知识. SVM是新近出现的强大的数据挖掘工具,它在文本分类.手写文字识别.图像分类.生物序列分析等实 ...