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 i o o i o
]

where i stands for Push and o stands for Pop. Your program should, given pairs of words produce sequences of stack operations which convert the first word to the second.

Input

The input will consist of several lines of input. The first line of each pair of input lines is to be considered as a source word (which does not include the end-of-line character). The second line (again, not including the end-of-line character) of each pair is a target word. The end of input is marked by end of file.

Output

For each input pair, your program should produce a sorted list of valid sequences of i and o which produce the target word from the source word. Each list should be delimited by

[
]

and the sequences should be printed in "dictionary order". Within each sequence, each i and o is followed by a single space and each sequence is terminated by a new line.

Process

A stack is a data storage and retrieval structure permitting two operations:

Push - to insert an item and
Pop - to retrieve the most recently pushed item

We will use the symbol i (in) for push and o (out) for pop operations for an initially empty stack of characters. Given an input word, some sequences of push and pop operations are valid in that every character of the word is both pushed and popped, and furthermore, no attempt is ever made to pop the empty stack. For example, if the word FOO is input, then the sequence:

i i o i o o is valid, but
i i o is not (it's too short), neither is
i i o o o i (there's an illegal pop of an empty stack)

Valid sequences yield rearrangements of the letters in an input word. For example, the input word FOO and the sequence i i o i o oproduce the anagram OOF. So also would the sequence i i i o o o. You are to write a program to input pairs of words and output all the valid sequences of i and o which will produce the second member of each pair from the first.

Sample Input

madam
adamm
bahama
bahama
long
short
eric
rice

Sample Output

[
i i i i o o o i o o
i i i i o o o o i o
i i o i o i o i o o
i i o i o i o o i o
]
[
i o i i i o o i i o o o
i o i i i o o o i o i o
i o i o i o i i i o o o
i o i o i o i o i o i o
]
[
]
[
i i o i o i o o
]

深度优先搜索即可。注意输出格式,每个i或者o后面都有空格

AC Code:
 #include <iostream>
#include <stack>
#include <cstdio>
#include <cstring> using namespace std; const int MAX_LEN = ;
char s[MAX_LEN], d[MAX_LEN], ans[MAX_LEN];
stack<char> sta; void DFS(int si, int di, int ansi) //三个参数分别是s,d,ans的下标
{
if(s[si] == '\0')
{
if(di == si)
{
for(int i = ; i < ansi; i++)
printf("%c ", ans[i]);
puts("");
return ;
}
else
{
if(!sta.empty() && sta.top() == d[di])
{
char t = sta.top();
sta.pop();
ans[ansi] = 'o';
DFS(si, di + , ansi + );
sta.push(t);
return ;
}
else return ;
}
}
sta.push(s[si]);
ans[ansi] = 'i';
DFS(si + , di, ansi + );
if(!sta.empty()) sta.pop();
if(!sta.empty() && sta.top() == d[di])
{
char t = sta.top();
sta.pop();
ans[ansi] = 'o';
DFS(si, di + , ansi + );
sta.push(t);
return ;
}
} int main()
{
while(scanf("%s %s", s, d) != EOF)
{
int lens = strlen(s);
int lend = strlen(d);
if(lens != lend)
{
printf("[\n]\n");
}
else
{
while(!sta.empty()) sta.pop();
printf("[\n");
DFS(, , );
printf("]\n");
}
}
return ;
}

Anagrams by Stack(深度优先搜索)的更多相关文章

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

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

  2. castle problem——(深度优先搜索,递归实现和stack实现)

    将问题的各状态之间的转移关系描述为一个图,则深度优先搜索遍历整个图的框架为:Dfs(v) {if( v 访问过)return;将v标记为访问过;对和v相邻的每个点u: Dfs(u);}int main ...

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

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

  4. ZOJ 1004 Anagrams by Stack

    Anagrams by Stack 题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1004 题意:通过堆栈实现将一 ...

  5. 深度优先搜索(DFS)

    定义: (维基百科:https://en.wikipedia.org/wiki/Depth-first_search) 深度优先搜索算法(Depth-First-Search),是搜索算法的一种.是沿 ...

  6. 图的遍历之深度优先搜索(DFS)

    深度优先搜索(depth-first search)是对先序遍历(preorder traversal)的推广.”深度优先搜索“,顾名思义就是尽可能深的搜索一个图.想象你是身处一个迷宫的入口,迷宫中的 ...

  7. stack+DFS ZOJ 1004 Anagrams by Stack

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

  8. HDU ACM 1515 Anagrams by Stack

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

  9. 深度优先搜索(DFS)——部分和问题

    对于深度优先搜索,这里有篇写的不错的博客:DFS算法介绍 .总得来说是从某个状态开始,不断的转移状态知道无法转移,然后回到前一步的状态.如此不断的重复一直到找到最终的解.根据这个特点,常常会用到递归. ...

随机推荐

  1. texbbox,combobox设置属性

    --输入框 $("#xx").textbox('setValue','value');  //设置输入框的值 $('#xx').textbox('textbox').attr('r ...

  2. linux mysql表名大小写

    1.用ROOT登录,修改/etc/my.cnf 2.在[mysqld]下加入一行:lower_case_table_names=1 0:区分大小写,1:不区分大小写 3.重新启动数据库即可

  3. 第88天:HTML5中使用classList操作css类

    在HTML5 API里,页面DOM里的每个节点上都有一个classList对象,程序员可以使用里面的方法新增.删除.修改节点上的CSS类.使用classList,程序员还可以用它来判断某个节点是否被赋 ...

  4. HDU4787_GRE Words Revenge

    这个题目做得泪牛满面. 题目为给你若干串,有的表示添加一个串,有的表示询问一个串有多少个字串为前面出现过的串. 题目一看就知道肯定是AC自动机(不过后缀自动机也是可以的) 但是细想就会发现AC自动机好 ...

  5. bzoj2788-Festival

    题意 有 \(n\) 个变量,有两种限制,分别有 \(m_1,m_2\) 种.限制如下: \(a_x+1=a_y\) \(a_x\le a_y\) 求 \(\{x_i\}\) 集合的大小.\(n\le ...

  6. bzoj4568-幸运数字

    题目 给出一棵树,每个节点上有权值\(a_i\),多次询问一条路径上选择一些点权值异或和最大值.\(n\le 2\times 10^4,q\le 2\times 10^5,0\le a_i\le 2\ ...

  7. 洛谷 P1495 曹冲养猪

    这是一道标准的孙子定理的题,题意浅显,思路明确 然后我就交了整整16遍啊,欺负人啊,题解暴力就能过,我就TLE ..悲惨的提交记录 下面是题面 题目描述 自从曹冲搞定了大象以后,曹操就开始捉摸让儿子干 ...

  8. P3386 【模板】二分图匹配

    题目背景 二分图 题目描述 给定一个二分图,结点个数分别为n,m,边数为e,求二分图最大匹配数 输入输出格式 输入格式: 第一行,n,m,e 第二至e+1行,每行两个正整数u,v,表示u,v有一条连边 ...

  9. Omeed 线段树

    目录 题面 题解 代码 题面 2.12 - - - 题解 大概还是挺妙的? 首先基础分和连击分互不干扰,所以可以分开统计. 基础分的统计比较简单,等于: \[A \sum_{i = l}^{r} p_ ...

  10. 【BZOJ2115】Xor(线性基)

    [BZOJ2115]Xor(线性基) 题面 BZOJ Description Input 第一行包含两个整数N和 M, 表示该无向图中点的数目与边的数目. 接下来M 行描述 M 条边,每行三个整数Si ...