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. error C2065: “uint8_t”: 未声明的标识符

    转载:https://blog.csdn.net/lys07962000/article/details/12052571 参考: http://blog.csdn.net/chenxin_130/a ...

  2. sprintf_s() 、sprintf()和printf()区别和用法

    转载:https://blog.csdn.net/qq_35608277/article/details/80878802 int sprintf_s(char *buffer,size_t size ...

  3. hashCode()方法源码分析

    执行代码 public class Demo06 { public static void main(String[] args) { String s="hello"; Syst ...

  4. MeteoInfoLab脚本示例:计算不同区域平均值

    这里用美国做例子,有一个美国区域的格点温度场数据(usgrid.data),需要计算出每个州(state)的平均温度.当然需要有一个包含各州行政区域的shape文件了(相关文件可以在此帖中下载:htt ...

  5. spring boot:构建多模块项目(spring boot 2.3.1)

    一,为什么要使用多模块? 1,结构更清晰,方便管理    如果只是一个小项目当然没有问题,    但如果功能越增越多则管理越来越复杂,    多模块可以使项目中模块间的结构分离   2,把项目划分成多 ...

  6. php中,posix_getpid() 和 getmypid() 有什么不同

    getmypid:windows 和 linux都可以用posix_getpid:仅linux可以用

  7. Archery安装教程

    一. CentOS设置 1. 更换阿里源 curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos ...

  8. mysql复制一个表到其他数据库

    db1为原数据库,db2为要导出到的数据库,fromtable 是要导出的表名1.方法一:登录导出到的数据库,执行create table fromtable select * from db1.fr ...

  9. salesforce零基础学习(九十七)Big Object

    本篇参考: https://developer.salesforce.com/docs/atlas.en-us.224.0.bigobjects.meta/bigobjects/async_query ...

  10. day05 selenium基本使用

    本文通过举例介绍selenium的基本使用方法,用来爬取京东笔记本电脑的商品信息,包括名称,url,价格,评价信息. from selenium import webdriver # 导入键盘Keys ...