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 #include <vector>
12 using namespace std;
13
14 const int MAXN = 1e4 + 10;
15 const int INF = 0x3f3f3f3f;
16
17 string s1, s2;
18 int len;
19 stack<char> S;
20 vector<char> V;
21
22 void DFS(int push, int pop)
23 {
24     if (push == len && pop == len)
25     {
26         for (int i=0; i<V.size (); ++i)
27             cout << V[i] << " ";
28         cout << endl;
29     }
30
31     if (push + 1 <= len)        //入栈
32     {
33         S.push (s1[push]);
34         V.push_back (‘i‘);
35         DFS (push+1, pop);
36         S.pop ();
37         V.pop_back ();
38     }
39
40     if (pop + 1 <= push && pop + 1 <= len && S.top () == s2[pop])        //出栈
41     {
42         char ch = S.top ();
43         S.pop ();
44         V.push_back (‘o‘);
45         DFS (push, pop+1);
46         S.push (ch);
47         V.pop_back ();
48     }
49 }
50
51 int main(void)        //ZOJ 1004 Anagrams by Stack
52 {
53     //freopen ("ZOJ_1004.in", "r", stdin);
54
55     while (cin >> s1 >> s2)
56     {
57         V.clear ();
58         while (!S.empty ())    S.pop ();
59
60         len = s1.length ();
61
62         cout << "[" << endl;
63         DFS (0, 0);
64         cout << "]" << endl;
65     }
66
67     return 0;
68 }
69
70 /*
71 [
72 i i i i o o o i o o
73 i i i i o o o o i o
74 i i o i o i o i o o
75 i i o i o i o o i o
76 ]
77 [
78 i o i i i o o i i o o o
79 i o i i i o o o i o i o
80 i o i o i o i i i o o o
81 i o i o i o i o i o i o
82 ]
83 [
84 ]
85 [
86 i i o i o i o o
87 ]
88 */
时间: 2025-01-05 14:54:00

stack+DFS ZOJ 1004 Anagrams by Stack的相关文章

ZOJ 1004 Anagrams by Stack

Anagrams by Stack 题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1004 题意:通过堆栈实现将一个字符串转变成目标字符串的操作,要求输出全部的可能操作组合. 思路:利用深度优先的搜索思路,对于每一个状态都有入栈和出栈两种可能的操作,由于要求按字典序输出,每次先考虑入栈再考虑出栈.即"能入就入,不能入考虑是否能退,随后返回上一步". 下面贴代码: 1 //Problem Name: A

[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 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

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&

[dfs] zoj 3736 Pocket Cube

题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3736 Pocket Cube Time Limit: 2 Seconds      Memory Limit: 65536 KB Pocket Cube is a 3-D combination puzzle. It is a 2 × 2 × 2 cube, which means it is constructed by 8 mini-cubes. For

[迭代加深dfs] zoj 3768 Continuous Login

题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3768 Continuous Login Time Limit: 2 Seconds      Memory Limit: 131072 KB      Special Judge Pierre is recently obsessed with an online game. To encourage users to log in, this game wi

[数学+dfs] ZOJ 3753 Simple Equation

题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5176 Simple Equation Time Limit: 2 Seconds      Memory Limit: 65536 KB There are many Equations. Some are difficult to solve, for example: an xn + an-1 xn-1 + .. + a0 = 0. In this probl

Azure Stack技术深入浅出系列3: Azure Stack运维工具Azure Stack Tools的使用及实战

源自 Azure 的 Azure stack 作为一款业界唯一的和领先的公有云平台一致的混合云平台,能够帮助企业客户从自有数据中心交付 Azure 云服务.作为<Azure Stack 技术深入浅出系列>的第三篇文章,本文将介绍 Azure Stack 部署完成以后,如何快速的管理和维护这套系统环境,包括虚拟机的管理.远程 VPN 连接.传输部署镜像.远程监控等.本文将通过一套 Azure Stack 运维工具集--Azure Stack Tools 来尝试使用和管理 Azure Stack