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. AD技巧之原理图元器件统一重新编号

    本文将简要介绍Altium Designer中如何进行原理图元器件统一命名,这是Altium Designer软件一个小技巧,在学习和工程实践中,都十分有用的技能. 第一步:打开原理图 第二步:点击& ...

  2. 对ACE和ATL积分

    下载source code - 39.66 KB 介绍 这篇文章展示了一种结合ACE和ATL的方法.它不打算作为功能演示,而是作为一个小型的"入门"解决方案,展示实现此目标的可行方 ...

  3. Informatica报错“表或视图不存在”的某种原因

    软件版本:9.6.1 背景:测试将OLTP数据库的用户信息表(CUST_INFO)抽取到DW库(DW_CUST_INFO) 问题:工作流启动后,报错RR_4035,并告知表或视图不存在 分析:在导入源 ...

  4. docker: Error response from daemon: Get https://registry-1.docker.io/v2/: net/http: request canceled

    故障描述: [root@docker01 ~]# docker run centos docker: Error response from daemon: Get https://registry- ...

  5. Ubuntu 20.04上通过Wine 安装微信

    没有想过会在一个手机软件上花这么多心思,好在今天总算安装成功,觉得可以记录下这个过程,方便他人方便自己. 首先介绍下我使用过的其他方法,希望可以节省大家一些时间: Rambox Pro:因为原理是网页 ...

  6. [论文阅读笔记] GEMSEC,Graph Embedding with Self Clustering

    [论文阅读笔记] GEMSEC: Graph Embedding with Self Clustering 本文结构 解决问题 主要贡献 算法原理 参考文献 (1) 解决问题 已经有一些工作在使用学习 ...

  7. 【idea】重装系统(格式化C盘后)idea无法导入maven(jdk重装了,版本不同),结果报错!

    [以下部分截图]2019-11-25 09:48:49,045 [ 108964]   WARN -      #org.jetbrains.idea.maven - Cannot open inde ...

  8. logback.xml demo

    如何关闭 org.apache.zookeeper.clientcnxn 的(控制台大量输出)debug 日志 1.在项目resources路径下新建 logback.xml 2.然后把下面的代码co ...

  9. PHP之Trait详解 转

    php从以前到现在一直都是单继承的语言,无法同时从两个基类中继承属性和方法,为了解决这个问题,php出了Trait这个特性 用法:通过在类中使用use 关键字,声明要组合的Trait名称,具体的Tra ...

  10. spring boot: 用redis的消息订阅功能更新应用内的caffeine本地缓存(spring boot 2.3.2)

    一,为什么要更新caffeine缓存? 1,caffeine缓存的优点和缺点 生产环境中,caffeine缓存是我们在应用中使用的本地缓存, 它的优势在于存在于应用内,访问速度最快,通常都不到1ms就 ...