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 2018 新版 NavigationView 尝鲜

    本文参考了官方文档以及提供的示例代码(官方代码貌似有点误导,所以写了这一篇,并且文末有GayHub代码地址) 官方文档发布于20180806,说明NavigationView刚发布了没几天,还在开发中 ...

  2. Linux的1000个命令

    目录 Linux常用命令 uptime wget uname free who last history pwd cd ls cat head tail tr wc cut diff touch mk ...

  3. Linux防火墙配置与管理(16)

    防火墙指的是一个由软件和硬件设备组合而成.在内部网和外部网之间.专用网与公共网之间的边界上构造的保护屏障.是一种获取安全性方法的形象说法,它是一种计算机硬件和软件的结合,使Internet与Intra ...

  4. python -猜字小游戏

    代码运行效果如下: 注意: 1.必须要在python3环境想使用 2.QQ:3084276329(一起交流学习) 3.还请大家评论 Guess the word game代码如下: #! /usr/b ...

  5. vscode 学习笔记 —— 重构

    一.vscode 自带 1.提取变量 2.提取方法 上面都是通过选中文本后出现的小灯泡操作的: 3.全局替换(多个文件中的)某个变量 选中变量按 F2,输入完成后按回车 二.vscode 插件 js- ...

  6. Django 模板相关

    Django 模板相关 视图函数只是直接返回文本,而在实际生产环境中其实很少这样用,因为实际的页面大多是带有样式的HTML代码,这可以让浏览器渲染出非常漂亮的页面.目前市面上有非常多的模板系统,其中最 ...

  7. Android中Serializable和Parcelable序列化对象详解

    学习内容: 1.序列化的目的 2.Android中序列化的两种方式 3.Parcelable与Serializable的性能比较 4.Android中如何使用Parcelable进行序列化操作 5.P ...

  8. Android _关于fragment切换重新加载的解决分享给大家

    在项目中需要进行Fragment的切换,一直都是用replace()方法来替换Fragment但是,这样会有一个问题 ,应该很多朋友都遇到过:每次切换的时候,Fragment都会重新实例化,也就是运行 ...

  9. JLS中表达式的所有文法

    3.8. Identifiers Identifier: IdentifierChars but not a Keyword or BooleanLiteral or NullLiteral Iden ...

  10. maven install 打包 报错 Cannot run program "gpg.exe": CreateProcess error

    打包报错, mvn install后加上参数-Dgpg.skip,例如:mvn install -Dgpg.skip   即可解决. 我们也可以去掉 这个 插件   <plugin>    ...