Anagrams by Stack

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1004

题意:通过堆栈实现将一个字符串转变成目标字符串的操作,要求输出全部的可能操作组合。

思路:利用深度优先的搜索思路,对于每一个状态都有入栈和出栈两种可能的操作,由于要求按字典序输出,每次先考虑入栈再考虑出栈。即“能入就入,不能入考虑是否能退,随后返回上一步”。

下面贴代码:

 1 //Problem Name: Anagrams by Stack
2 //Source: ZOJ 1004
3 //Author: jinjin18
4 //Main idea: DFS
5 //Language: C++
6 //=========================================================
7 #include<stdio.h>
8 #include<string.h>
9
10 char origin[1000];
11 char target[1000];
12 char temp[1000];
13 int top = -1;
14 char opt[2005];
15 int iopt;
16 int popn,pushn;
17 int len;
18 void DFS(){
19 if(popn == len){
20 for(int i = 0; i < 2*len; i++){
21 printf("%c ",opt[i]);
22 }
23 printf("\n");
24 return ;
25 }
26
27 if(pushn < len){
28 top++;
29 temp[top] = origin[pushn];
30 pushn++;
31 opt[iopt] = 'i';
32 iopt++;
33 DFS();
34 iopt--;
35 top--;
36 pushn--;
37 }
38
39 if(popn < pushn&& temp[top] == target[popn]){
40 top--;
41 popn++;
42 opt[iopt] = 'o';
43 iopt++;
44 DFS();
45 iopt--;
46 top++;
47 popn--;
48 temp[top] = target[popn]; //恢复现场
49 }
50 return ;
51 }
52
53 int main(){
54
55
56 while(scanf("%s%s",origin,target)!=EOF){
57 popn = pushn = 0;
58 iopt = 0;
59 len = strlen(origin);
60 printf("[\n");
61 DFS();
62 printf("]\n");
63 }
64
65 return 0;
66 }

ZOJ 1004 Anagrams by Stack的更多相关文章

  1. stack+DFS ZOJ 1004 Anagrams by Stack

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

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

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

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

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

  4. [ZJU 1004] Anagrams by Stack

    ZOJ Problem Set - 1004 Anagrams by Stack Time Limit: 2 Seconds      Memory Limit: 65536 KB How can a ...

  5. 1004 Anagrams by Stack

    考察DFS的应用,用栈描述字符串的变化过程. #include <stdio.h> #include <string.h> int len1,len2; ],str2[],st ...

  6. Anagrams by Stack(深度优先搜索)

    ZOJ Problem Set - 1004 Anagrams by Stack Time Limit: 2 Seconds      Memory Limit: 65536 KB How can a ...

  7. HDU ACM 1515 Anagrams by Stack

    Anagrams by Stack Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  8. 【Acm】算法之美—Anagrams by Stack

    题目概述:Anagrams by Stack How can anagrams result from sequences of stack operations? There are two seq ...

  9. 深搜———ZOJ 1004:anagrams by stack

    细节问题各种虐!! 其实就是简单的一个深搜 看成二叉树来理解:每个节点有两个枝:入栈和出栈. 剪枝操作:只有当栈顶元素和当前位置的目标字符相同时才出栈,否则就不出栈 dfs写三个参数:depth搜索深 ...

随机推荐

  1. Linux 串口工具 lsz lrz 移植

    //之前写的,刚才不小心误删了,所以重新再发出来. 1 下载源码包 首先下载最新版的lrzsz,地址:https://ohse.de/uwe/software/lrzsz.html.下面以 0.12. ...

  2. Layman ThinkPHP 中 where条件 or,and 同时使用

    Eg:('a'=1 and 'b'=2) or ('c'=3 and 'd'=4) and 'e'=5 解决方法 $condition1['a'] = 1; $condition1['b'] = 2; ...

  3. 022 01 Android 零基础入门 01 Java基础语法 03 Java运算符 02 算术运算符

    022 01 Android 零基础入门 01 Java基础语法 03 Java运算符 02 算术运算符 本文知识点:Java中的算术运算符 算术运算符介绍 算术运算符代码示例 注意字符串连接问题和整 ...

  4. 在SpringBoot项目中怎样引入.yml文件中的设置

    SpringBoot中获取application.yml文件内容 原始方式pro.load()与 pro.getProperty()配合的方式 @Value注解方式 @ConfigurationPro ...

  5. 多测师讲解ui自动化框架设计思想_高级讲师肖sir

    UI自动化框架:UI自动化框架可以分为8个模块,conf.data.public.pageobject.testcase.runner.report.log.conf是用来储存系统环境.数据库.邮件的 ...

  6. empty()和size() == 0有区别吗

    empty()和size() 这里说的empty()和size()都是STL的容器中提供的接口,分别用来判断当前容器是否为空和获取当前包含的元素个数 区别 其实按道理来说两者应该是相等的,而且STL容 ...

  7. 什么是 C 和 C ++ 标准库?学编程的你应该知道这些知识!

    简要介绍编写C/C ++应用程序的领域,标准库的作用以及它是如何在各种操作系统中实现的. 我已经接触C++一段时间了,一开始就让我感到疑惑的是其内部结构:我所使用的内核函数和类从何而来? 谁发明了它们 ...

  8. 【原创】xenomai内核解析--xenomai与普通linux进程之间通讯XDDP(一)--实时端socket创建流程

    版权声明:本文为本文为博主原创文章,转载请注明出处.如有问题,欢迎指正.博客地址:https://www.cnblogs.com/wsg1100/ 1.概述 上篇文章xenomai内核解析--实时IP ...

  9. jquery1.9+,jquery1.10+ 为什么不支持live方法了?

    live() 替换成 on() die()  替换成off() 根据jQuery的官方描述,live方法在1.7中已经不建议使用,在1.9中删除了这个方法.并建议在以后的代码中使用on方法来替代. o ...

  10. ReSharper 注册码

    用户名:ronle 注册码:ZoJzmeVBoAv9Sskw76emgksMMFiLn4NM 原文地址:http://hi.baidu.com/ronle/item/a509b5f7b851971be ...