想多了!以为一直dfs所有的情况会超时,所以直接忽略了,就自己想了一个优化的算法,最后测试结果对了,但是wa了,自己写算法很容易考虑不周的,还是在最后没有办法的时候在考虑自己的算法吧!!!简单的dfs就可以了!

#include<stdio.h>
#include<string.h>
#define Max 100 char goal_str[Max];
char source_str[Max];
char stack[Max];
int path[Max];
int lenth,top,pointer; void print_path(void)
{
int i;
for(i=0;i<2*lenth;i++)
{
if(path[i]==1)
printf("i ");
else
printf("o ");
}
printf("\n");
}
void dfs(int npush,int npop)
{
char tmp;
if(npush==lenth&&npop==lenth)
{
print_path();
return ;
}
if(npush<lenth)
{
path[pointer++]=1;
stack[top++]=source_str[npush];
dfs(npush+1,npop);
top--;
pointer--;
}
if(top>0&&stack[top-1]==goal_str[npop])
{
tmp=stack[top-1];
path[pointer++]=-1;
top--;
dfs(npush,npop+1);
pointer--;
top++;
stack[top-1]=tmp;
}
}
int main()
{
int n1;
while(scanf("%s%s",source_str,goal_str)!=EOF)
{
pointer=0;
top=0; printf("[\n"); lenth=strlen(source_str);
n1=strlen(goal_str); if(n1==lenth)
dfs(0,0); printf("]\n"); }
return 0;
}

  

zoj 1004 dfs的更多相关文章

  1. stack+DFS ZOJ 1004 Anagrams by Stack

    题目传送门 /* stack 容器的应用: 要求字典序升序输出,所以先搜索入栈的 然后逐个判断是否满足答案,若不满足,回溯继续搜索,输出所有符合的结果 */ #include <cstdio&g ...

  2. ZOJ 1004 Anagrams by Stack

    Anagrams by Stack 题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1004 题意:通过堆栈实现将一 ...

  3. ZOJ 1004 Anagrams by Stack(DFS+数据结构)

    Anagrams by Stack 题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4 题目大意:输入两个字符串序列,判 ...

  4. 【dfs 回溯】 zoj 1004

    题意:给出一个源字符串和一个目标字符串,打出所有符合stack操作的i,o串使得对于源字符串的操作能变为目标字符串 思路:搜索,回溯. 之前想过是不是队列,觉得不对那样bfs是求最优解了:也想过用结构 ...

  5. [ZOJ 1004] Anagrams by Stack (简单搜索)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1004 题目大意:给你个栈,给你源串和目标串,按字典序输出符合要求 ...

  6. POJ 1979 Red and Black (zoj 2165) DFS

    传送门: poj:http://poj.org/problem?id=1979 zoj:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problem ...

  7. HDU 1010 Tempter of the Bone (ZOJ 2110) DFS+剪枝

    传送门: HDU:http://acm.hdu.edu.cn/showproblem.php?pid=1010 ZOJ:http://acm.zju.edu.cn/onlinejudge/showPr ...

  8. POJ 1562 Oil Deposits (HDU 1241 ZOJ 1562) DFS

    现在,又可以和她没心没肺的开着玩笑,感觉真好. 思念,是一种后知后觉的痛. 她说,今后做好朋友吧,说这句话的时候都没感觉.. 我想我该恨我自己,肆无忌惮的把她带进我的梦,当成了梦的主角. 梦醒之后总是 ...

  9. ZOJ - 2477 dfs [kuangbin带你飞]专题二

    注意输入的处理,旋转操作打表.递增枚举可能步数,作为限制方便找到最短路. AC代码:90ms #include<cstdio> #include<cstring> char m ...

随机推荐

  1. RMA编码为必输

    应用 Oracle Inventory 层 Level Function 函数名 Funcgtion Name RCV_RCVTXERE 表单名 Form Name RCVTXERE 说明 Descr ...

  2. support.SerializationFailedException: Failed to deserialize payload.

    support.SerializationFailedException: Failed to deserialize payload. Is the byte array a result of c ...

  3. mciSendString用法

    使用MCI API,源文件中需要包含头文件 Mmsystem.h,在Project->Settings->Link->Object/libray module中加入库 Winmm.l ...

  4. 利用UIScrollView和UIPageControl实现多页图片欢迎页面

    在.h文件当中实现UIScrollViewDelegate协议,让控制器充当代理: #import <UIKit/UIKit.h> @interface RPRootViewControl ...

  5. gallery利用代码定位图片并且不丢失动画效果

    安卓中,利用gallery.setSelection(position);可以手动定位图片 但是众所周知会丢失动画效果 即使是用gallery.setSelection(position,true); ...

  6. Facebook、新浪微博、Twitter、腾讯微博分享代码

    最近总结一下用到的分享代码 FaceBook分享 <a href="https://www.facebook.com/sharer/sharer.php?u=你的链接" ta ...

  7. 关于重复记录和外部 ID (CRM导入提示已找到重复的查找引用)

    http://docs.huihoo.com/oracle/crm-on-demand/21/local/html/Release21_SimpleChinese/index.htm?toc.htm? ...

  8. PDF417码制尺寸定义

    PDF417码制尺寸定义         1.模块宽度(X)不得小于0.191mm,你们目前所用300dpi打印机点的尺寸是0.084 mm * 0.01 mm,可  三倍以上值        2.条 ...

  9. ring3 dll hide

    ZwQuerySystemInformation(SystemProcessInformation,SystemInformation,Length,ReturnLength);         pS ...

  10. Svn入门

    1.建立svn仓库 ›    命令svnadmin create 仓库名称,如:进入命令行窗口,切换到Svn安装目录下,输入如下命令:svnadmin create F:\software\repos ...