Zipper(poj2192)dfs+剪枝
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 15277 | Accepted: 5393 |
Description
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
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
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+剪枝的更多相关文章
- HDU 1501 Zipper 【DFS+剪枝】
HDU 1501 Zipper [DFS+剪枝] Problem Description Given three strings, you are to determine whether the t ...
- HDOJ 1501 Zipper 【DP】【DFS+剪枝】
HDOJ 1501 Zipper [DP][DFS+剪枝] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Ja ...
- *HDU1455 DFS剪枝
Sticks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- POJ 3009 DFS+剪枝
POJ3009 DFS+剪枝 原题: Curling 2.0 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16280 Acce ...
- poj 1724:ROADS(DFS + 剪枝)
ROADS Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10777 Accepted: 3961 Descriptio ...
- DFS(剪枝) POJ 1011 Sticks
题目传送门 /* 题意:若干小木棍,是由多条相同长度的长木棍分割而成,问最小的原来长木棍的长度: DFS剪枝:剪枝搜索的好题!TLE好几次,终于剪枝完全! 剪枝主要在4和5:4 相同长度的木棍不再搜索 ...
- DFS+剪枝 HDOJ 5323 Solve this interesting problem
题目传送门 /* 题意:告诉一个区间[L,R],问根节点的n是多少 DFS+剪枝:父亲节点有四种情况:[l, r + len],[l, r + len - 1],[l - len, r],[l - l ...
- HDU 5952 Counting Cliques 【DFS+剪枝】 (2016ACM/ICPC亚洲区沈阳站)
Counting Cliques Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- HDU 5937 Equation 【DFS+剪枝】 (2016年中国大学生程序设计竞赛(杭州))
Equation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- LA 6476 Outpost Navigation (DFS+剪枝)
题目链接 Solution DFS+剪枝 对于一个走过点k,如果有必要再走一次,那么一定是走过k后在k点的最大弹药数增加了.否则一定没有必要再走. 记录经过每个点的最大弹药数,对dfs进行剪枝. #i ...
随机推荐
- 关于scanf、getchar、getch、getche缓冲区分析——C语言
缓冲区 根据数据刷新的时机可以将缓冲区的类型分为:全缓冲.行缓冲.无缓冲 (注意:Windows下的输出设备没有缓冲区,意思是printf是无缓冲的,但是在Linux下printf就是行缓冲的,至于为 ...
- svn 设置 excel 比对工具为 SPREADSHEETCOMPARE.EXE
http://blog.csdn.net/ccpat/article/details/50725774
- linux 时间相关
CentOS7 正确修改时区方法 ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
- 解决微信小程序要求的TLS版本必须大于等于1.2的问题
一.环境: CentOS 6.8 nginx 1.6.0 php 7.0.10 二.背景 最近开发一个小程序,而小程序对后台接口服务器的要求是: 1.请求域名在request合法域名中 2.基于 ht ...
- B2C电商项目
经历四个月的自学. 结合所学的知识(HTML,CSS,javascript,jQuery,Mysql,Redis,Django,celery,fastDfs,haystack,whoosh,uWSGI ...
- Python-doc rst文件打开
Python rst文件打开 RST与Python类似Javadoc与Java. 如果下载了别人的Python源码,里面有rst文件夹,我们可以转为html后用浏览器打开 某个开源项目的index.r ...
- 在MVC3中修改KindEditor实现上传图片到指定文件夹
KindEditor编辑器默认上传的图片文件夹,是根据系统时间自动生成的,图片是自动上传到这些文件夹里面,无法选择.如果要上传图片到指定文件夹,像相册一样管理图片,则需要扩展KindEditor编辑器 ...
- 读书笔记(03) - 性能 - JavaScript高级程序设计
作用域链查找 作用域链的查找是逐层向上查找.查找的层次越多,速度越慢.随着硬件性能的提升和浏览器引擎的优化,这个慢我们基本可以忽略. 除了层级查找损耗的问题,变量的修改应只在局部环境进行,尽量避免在局 ...
- 主键映射和Hibernate映射
组件映射 类组合关系的映射,也叫做组件映射! 注意:组件类和被包含的组件类,共同映射到一张表! 需求: 如汽车与车轮 代码示例: 1.JavaBean Wheel.java package com.g ...
- logstash-3-输出到es中
之前测试 filebeat和logstash的时候, 使用的是stdout标准输出, 现在我们想把数据输出到es中去, 1, 首先需要一个es: 修改配置文件 后台启动 ./bin/elasticse ...