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

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 Output Limit Exceeded的问题了N次。。。最后发现自己死循环了。 之前,剪枝没写好,可惜不能最后秒杀啊!o(︶︿︶)o 唉 具体的见代码吧, ps:http://poj.org/problem?id=2192
#include<stdio.h>
#include<string.h> char a[],b[],c[];
int visit[];
int lena,lenb,lenc;
bool flag; int check()//与c串匹配
{
int k,t=;
for(k=;k<lena;k++)
{
if(!visit[k]&&a[k]-c[t]==)
t++;
}
if(t==lenc)
return ;
else
return ;
} int dfs(int x,int y,int t)//x是b串的瞬时位置,y是a串的瞬时位置,t是c串的瞬时位置,
{
if(x>=lenb)
{
if(check())
flag=true;
return ;//返回
}
if(flag) //剪枝,flag已经为真
return ;
if(b[x]==a[y])
{
visit[y]=;
if(y<lena)
dfs(x+,y+,t);//匹配下一个
visit[y]=;
}
else
{
if(a[y]!=c[t]) //不在b串中的,肯定在c串中,否则退出!只是剪枝的关键
return ;
}
if(!flag && y<lena)// 不在b串中的,肯定在c串中,所以匹配c串
dfs(x,y+,t+);
return ;
}
int main()
{ int T,i,j;
scanf("%d",&T);
i=;
getchar();
while(i<=T)
{ scanf("%s%s%s",b,c,a);
memset(visit,,sizeof(visit));
lena=strlen(a);
lenb=strlen(b);
lenc=strlen(c);
printf("Data set %d: ",i);
int temp=;
flag=false;
//a串的最后一个字符要么是b串的最后一个,要么是c串的最后一个,都不是的话,应该输出no
if(a[lena-]!=b[lenb-] && a[lena-]!=c[lenc-])
flag=false;
else if(lena!=lenb+lenc)//若是b串和c串的长度之和不等于a串的话,应该输出no
flag=false; else//否则深搜
dfs(,,);
if(flag)
printf("yes\n");
else
printf("no\n");
i++;
}
} /*
50
cat tree tcraete
cat tree catrtee
cat tree cttaree
cat tree tcraeta
cat tree catree
cat tree cttaree
*/

Zipper(poj2192)dfs+剪枝的更多相关文章

  1. HDU 1501 Zipper 【DFS+剪枝】

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

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

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

  3. *HDU1455 DFS剪枝

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

  4. POJ 3009 DFS+剪枝

    POJ3009 DFS+剪枝 原题: Curling 2.0 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16280 Acce ...

  5. poj 1724:ROADS(DFS + 剪枝)

    ROADS Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10777   Accepted: 3961 Descriptio ...

  6. DFS(剪枝) POJ 1011 Sticks

    题目传送门 /* 题意:若干小木棍,是由多条相同长度的长木棍分割而成,问最小的原来长木棍的长度: DFS剪枝:剪枝搜索的好题!TLE好几次,终于剪枝完全! 剪枝主要在4和5:4 相同长度的木棍不再搜索 ...

  7. DFS+剪枝 HDOJ 5323 Solve this interesting problem

    题目传送门 /* 题意:告诉一个区间[L,R],问根节点的n是多少 DFS+剪枝:父亲节点有四种情况:[l, r + len],[l, r + len - 1],[l - len, r],[l - l ...

  8. HDU 5952 Counting Cliques 【DFS+剪枝】 (2016ACM/ICPC亚洲区沈阳站)

    Counting Cliques Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  9. HDU 5937 Equation 【DFS+剪枝】 (2016年中国大学生程序设计竞赛(杭州))

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

  10. LA 6476 Outpost Navigation (DFS+剪枝)

    题目链接 Solution DFS+剪枝 对于一个走过点k,如果有必要再走一次,那么一定是走过k后在k点的最大弹药数增加了.否则一定没有必要再走. 记录经过每个点的最大弹药数,对dfs进行剪枝. #i ...

随机推荐

  1. VS2015+MySql+EF6采坑经验总结

    背景:VS2015+MySql+EF6(DB First) 采坑顺序:按照以前的记忆,操作依次如下: 1,安装 MySQL Connector/NET(不用想,装最新的,8.0.12) 2.安装 My ...

  2. 背水一战 Windows 10 (55) - 控件(集合类): SemanticZoom, ISemanticZoomInformation

    [源码下载] 背水一战 Windows 10 (55) - 控件(集合类): SemanticZoom, ISemanticZoomInformation 作者:webabcd 介绍背水一战 Wind ...

  3. Spring AOP 源码分析 - 筛选合适的通知器

    1.简介 从本篇文章开始,我将会对 Spring AOP 部分的源码进行分析.本文是 Spring AOP 源码分析系列文章的第二篇,本文主要分析 Spring AOP 是如何为目标 bean 筛选出 ...

  4. activemq在一台服务器上启动多个Broker

    步骤如下: 1.把整个conf文件夹复制一份,比如叫conf2 2.修改里面的activemq.xml文件 ①brokerName不能和原来的重复 ②数据存放的文件名称不能重复,比如<kahaD ...

  5. Redis---ZipList(压缩列表)

    1.概述 压缩列表是一块连续的内存空间,元素之间紧挨着存储,没有任何冗余空间. Redis 为了节约内存空间使用,zset 和 hash 容器对象在元素个数较少的时候,采用压缩列表 (ziplist) ...

  6. 文件分享系统(Django)

  7. String.format(String format,Object... args)的用法

    String.format(String format, Object... args)方法详解 以前也看到过很多次这个用法,一直记不牢靠,今天整理一下.   我仅仅举几个例子稍做说明: String ...

  8. 微信正式开放内测“小程序”,不开发APP的日子真的来了?

    关注,QQ群,微信应用号社区 511389428 微信正式开放内测“小程序”,不开发APP的日子真的来了? 明星公司 缪定纯 • 2016-09-22 09:05 讨论了很久的微信应用号终于来了,不过 ...

  9. Struts框架核心工作流程与原理

    1.Struts2架构图  这是Struts2官方站点提供的Struts 2 的整体结构.  执行流程图 2.Struts2部分类介绍  这部分从Struts2参考文档中翻译就可以了. ActionM ...

  10. php -- 文件上传下载

    ----- 026-upload.php ----- <!DOCTYPE html> <html> <head> <meta http-equiv=" ...