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. UWP Button添加圆角阴影(三)

    原文:UWP Button添加圆角阴影(三) Composition DropShadow是CompositionAPI中的东西,使用Storyboard设置某个属性,就是频繁的触发put_xxx() ...

  2. 【转】Windows Server 2016 安装 IIS 服务时提示指定备用源路径

    原文地址:http://www.codingwhy.com/view/973.html 在Windows Serever 2016中安装IIS的时候,遇到以下提示 是否需要指定备用源路径?一个或多个安 ...

  3. 一步一步教你用 Vue.js + Vuex 制作专门收藏微信公众号的 app

    一步一步教你用 Vue.js + Vuex 制作专门收藏微信公众号的 app 转载 作者:jrainlau 链接:https://segmentfault.com/a/1190000005844155 ...

  4. solr初识

    参考资料http://blog.csdn.net/l1028386804/article/details/70199983

  5. jvm垃圾回收的过程

    垃圾回收的过程分为两步: 1.判断对象是否死亡 (1)引用计数器法: ①每当有一个对象引用是,计数器加一,当计数器为0是对象死亡 ②缺点:无法解决循环引用的问题,假设A引用B,B引用A,那么这两个对象 ...

  6. iOS-iOS9系统SEGV_ACCERR问题处理【v3.6.3的一些bug修复】

    前言 最近APP不断地更新版本,却发现一些未知的错误导致崩溃,我把能测出来的错误,全部修复了,因为项目里集成了腾讯Bugly,看了下后台的崩溃,依旧千篇一律啊,然后就纠结了,很多SEGV_ACCERR ...

  7. POJ 2521

    #include <iostream> #include <stdio.h> using namespace std; int main() { //freopen(" ...

  8. POJ 2498

    #include<iostream> using namespace std; #include<string> #include<stdio.h> int mai ...

  9. 关于Mybatis中Mapper是使用XML还是注解的一些思考

    XML 据说可以灵活的进行注解,但是修改以后还是要重新发布程序.当然,你可以说,在Tomcat中改了,然后热加载了,不就可以了.可是一般情况下都是几台,十几台服务器.都是用发布系统,持续集成的方式部署 ...

  10. 57.storm拓扑结构调整

    几个概念 Topology(拓扑):Spout.Bolt组成的一个完整的流程结构: Stream Grouping:流分组.数据的分发方式: Spout:直译 水龙头,也就是 消息源 的意思: Bol ...