ZOJ 1004 Anagrams by Stack

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 }

原文地址:https://www.cnblogs.com/jinjin-2018/p/8972358.html

时间: 2024-08-13 07:14:42

ZOJ 1004 Anagrams by Stack的相关文章

stack+DFS ZOJ 1004 Anagrams by Stack

题目传送门 1 /* 2 stack 容器的应用: 要求字典序升序输出,所以先搜索入栈的 3 然后逐个判断是否满足答案,若不满足,回溯继续搜索,输出所有符合的结果 4 */ 5 #include <cstdio> 6 #include <iostream> 7 #include <algorithm> 8 #include <stack> 9 #include <cmath> 10 #include <cstring> 11 #inc

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

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1004 题目大意:给你个栈,给你源串和目标串,按字典序输出符合要求的进站出站序列. 就是搜搜搜呗... 带上答案和模拟的栈.. 代码: 1 #include <cstdio> 2 #include <cstdlib> 3 #include <string> 4 #include <iostream> 5 #include &l

[JAVA][ZOJ 1004][Anagrams by Stack]

[java] view plaincopyprint? import java.io.BufferedInputStream; import java.util.Scanner; public class Main { static String start;// record the first str static String end;// record the rearranged str public static void dfs(char[] stack, char[] seque

ZOJ Problem Set - 1004 Anagrams by Stack (回溯法)

ZOJ Problem Set - 1004 Anagrams by Stack Time Limit: 2 Seconds      Memory Limit: 65536 KB How can anagrams result from sequences of stack operations? There are two sequences of stack operators which can convert TROT to TORT: [ i i i i o o o o i o i

1004 Anagrams by Stack

考察DFS的应用,用栈描述字符串的变化过程. 1 #include <stdio.h> 2 #include <string.h> 3 int len1,len2; 4 char str1[100],str2[100],stk[100],ans[200]; 5 6 void output(int n){ 7 int i; 8 for(i=0;i<n;i++){ 9 printf("%c ",ans[i]); 10 } 11 printf("\n&

深搜———ZOJ 1004:anagrams by stack

细节问题各种虐!! 其实就是简单的一个深搜 看成二叉树来理解:每个节点有两个枝:入栈和出栈. 剪枝操作:只有当栈顶元素和当前位置的目标字符相同时才出栈,否则就不出栈 dfs写三个参数:depth搜索深度,npush压栈数,npop出栈数 npush用于记录压栈数:主要判断当前压栈是否合理,以及要压入的元素在原串种的位置 npop用于记录出栈数:判断生成的目标串元素的位置 当npush==npop==目标串时,说明生成了一个可执行的操作串 注意输出操作串的时候一定要用depth参数来控制,因为在多

12、Anagrams by Stack

How can anagrams result from sequences of stack operations? There are two sequences of stack operators which can convert TROT to TORT: [ i i i i o o o o i o i i o o i o ] where i stands for Push and o stands for Pop. Your program should, given pairs

zoj题目分类

饮水思源---zoj 转载自:http://bbs.sjtu.edu.cn/bbscon,board,ACMICPC,file,M.1084159773.A.html 注:所有不是太难的题都被归成了“简单题”,等到发现的时候已经太晚了,我太死脑筋 了……:( 有些题的程序我找不到了,555……:( SRbGa的题虽然都很经典……但是由于其中的大部分都是我看了oibh上的解题报告后做 的,所以就不写了…… 题目排列顺序没有规律……:( 按照个人感觉,最短路有的算做了DP,有的算做了图论. 有些比较

POJ百道水题列表

以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight Moves1101 Gamblers1204 Additive equations 1221 Risk1230 Legendary Pokemon1249 Pushing Boxes 1364 Machine Schedule1368 BOAT1406 Jungle Roads1411 Annive