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

Source: Zhejiang University Local Contest 2001


Solution:

  暴力模拟

  每次先搜索push,再判断能否pop。

  回溯时需注意。

#include<bits/stdc++.h>
using namespace std;
char a[],b[];
int al;
int bl;
stack<char> T;
int opt[];
void DFS(int dep,int x,int y)
{
//cout << dep << ' ' << x << ' ' << y << endl;
if (x == al && y == bl)
{
//A successful opt.
for (int i=;i<=al+bl;i++)
{
if (opt[i] == )
cout << "i ";
else cout << "o ";
}
cout << endl;
return;
}
if (x != al)
{
opt[dep]=;
T.push(a[x]);
DFS(dep+,x+,y);
T.pop();
}
if (!T.empty() && y != bl && T.top() == b[y])
{
char tmp = T.top();
T.pop();
opt[dep]=;
DFS(dep+,x,y+);
T.push(tmp);
}
return;
}
void solve()
{
al = strlen(a);
bl = strlen(b);
while (!T.empty()) T.pop();
memset(opt,,sizeof(opt));
cout << '[' << endl;
DFS(,,);
cout << ']' << endl; }
int main()
{
while (cin >> a >> b)
solve();
}

[ZJU 1004] Anagrams by Stack的更多相关文章

  1. stack+DFS ZOJ 1004 Anagrams by Stack

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

  2. ZOJ 1004 Anagrams by Stack

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

  3. ZOJ 1004 Anagrams by Stack(DFS+数据结构)

    Anagrams by Stack 题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4 题目大意:输入两个字符串序列,判 ...

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

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

  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. Matlab与C++混合编程 1--在C++中调用自己写的matlab函数

    在Visual Studio中使用C++中调用MATLAB程序 在matlab中可以通过mbuild工具将.m文件编译成dll文件供外部的C++程序调用,这样就可以实现matlab和C++混合编程的目 ...

  2. ARP协议基础

    ARP 什么是ARP协议 ARP协议是能够根据IP地址解析出该IP地址所在设备的MAC地址,叫(Address Resolution Protocol)地址解析协议 ARP地址的工作流程 当一台主机A ...

  3. 第九周课程总结&实验报告七

    实验任务详情: 完成火车站售票程序的模拟. 要求: (1)总票数1000张: (2)10个窗口同时开始卖票: (3)卖票过程延时1秒钟: (4)不能出现一票多卖或卖出负数号票的情况. package ...

  4. Git入门资料

    1.廖雪峰老师Git教程 地址:https://www.liaoxuefeng.com/wiki/896043488029600 2.Eclipse eGit连接GitHub教程 地址:https:/ ...

  5. [转帖]同事推荐的的aira2

    Windows系统安装最新版Aria2客户端及使用教程 https://www.moerats.com/archives/519/ 改天学习一下. 说明:之前都是说的在Linux VPS服务器上安装A ...

  6. RocketMQ事务性消息及持久化

    TransactionProducer(事务消息): 在分布式系统中,我们时常会遇到分布式事务的问题,除了常规的解决方案之外,我们还可以利用RocketMQ的事务性消息来解决分布式事务的问题.Rock ...

  7. java 数组详细介绍

    一.概述 数组(Array),是多个相同类型数据按一定顺序排列的集合,并使用一个名字命名,并通过编号的方式对这些数据进行统一管理 数组常见概念: 数组名, 下标(或索引), 元素, 数组的长度 数组本 ...

  8. 《剑指offer》面试题26 复杂链表的复制 Java版

    (定义一个新的数据结构,每个节点除了具有普通链表的next域外,还有一个额外的引用指向任意节点.我们要对由该特殊数据结构形成的链表进行复制.) 我的方法:也就是克隆一个这种特殊链表,很快想到先不考虑原 ...

  9. P1313计算系数

    这是2011年提高组第一题,一个数论题.如果当年我去的话,就爆零了wuwuwu. 题目:(ax+by)^k中询问x^m*y^n这一项的系数是多少?拿到题我就楞了,首先便是想到DP,二维分别存次数代表系 ...

  10. js获取url中的参数(解决中文乱码)

    这个是封装好的方法: function getQueryString(name) { var reg = new RegExp("(^|&)" + name + " ...