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 ... 
随机推荐
- C++数组初始化方法
			定义: ]; // array of 10 uninitialized ints 此 new 表达式分配了一个含有 10 个 int 型元素的数组,并返回指向该数组第一个元素的指针,此返回值初始化了指 ... 
- AWD比赛常规套路
			作者:Leafer 本文属于Arctic shell原创内容计划文章,转载请注明原文地址! 比赛环境:纯净win10,最新版kali,securecrt或者WinSCP 在进入服务器后应进行的常规 ... 
- spring mvc开发过程中的乱码问题
			在保证jsp,xml,数据库,编辑器编码一致的情况下. 1,用户输入中文,后台接收到也是中文,但是保存到数据库时乱码, 解决方法: 链接数据库的url="jdbc:mysql://local ... 
- 简单介绍RPM包制作方法
			RPM是RedHat Package Manager(RedHat软件包管理工具)的缩写,是一种用于互联网下载包的打包及安装工具,它包含在某些Linux分发版中.它生成具有.RPM扩展名的文件.使用r ... 
- 从用户浏览器输入url到用户看到页面结果的过程,发生了什么事情?
			1.域名解析 域名解析的过程: 1).查询浏览器自身DNS缓存 2).若上面没有查找到,则搜索操作系统自身的dns缓存 3).若上面没有找到,则尝试读取hosts文件 4).若上面没有找到,向本地配 ... 
- MapReduce中的倒排索引
			0.倒排索引资料: http://blog.csdn.net/pzasdq/article/details/51442856 1.三个日志源文件: a.txt hello tom hello jerr ... 
- Java操作word文档使用JACOB和POI操作word,Excel,PPT需要的jar包
			可参考文档: http://wibiline.iteye.com/blog/1725492 下载jar包 http://download.csdn.net/download/javashixiaofe ... 
- Postman入门之Mock测试
			1.什么是Mock测试: mock测试就是在测试过程中,对于某些不容易构造或者不容易获取的对象,用一个虚拟的对象来创建以便测试的测试方法. 2.添加要Mock测试的接口为example: 2.1点击r ... 
- Touch事件传递机制    Android
			Touch事件分发中只有两个主角:ViewGroup和View.Activity的Touch事件事实上是调用它内部的ViewGroup的Touch事件,可以直接当成ViewGroup处理. View在 ... 
- JVM内存结构(转)
			所有的Java开发人员可能会遇到这样的困惑?我该为堆内存设置多大空间呢?OutOfMemoryError的异常到底涉及到运行时数据的哪块区域?该怎么解决呢?其实如果你经常解决服务器性能问题,那么这些问 ... 
