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. 【ABAP系列】SAP ABAP 从FTP服务器读取文件到本地

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP 从FTP服务器 ...

  2. 【MM系列】SAP MM模块-BAPI:BAPI_GOODSMVT_CREATE的CODE分析

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[MM系列]SAP MM模块-BAPI:BAPI ...

  3. zimg 服务器配置文件

    --zimg server config --server config --是否后台运行 is_daemon = --绑定IP ip = '0.0.0.0' --端口 port = --运行线程数, ...

  4. Deepin15.10 python3安装、更新pip

    Deepin自带的Python3没有pip 需要安装一下: sudo apt install python3-pip更新pip sudo pip3 install --upgrade pip 然后运行 ...

  5. Element-ui 使用详细介绍

    一.后台搭建 使用 vue-admin-template 来快速搭建后台管理,它包含了 Element UI & axios & iconfont & permission c ...

  6. P1313计算系数

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

  7. centos7yum安装VirtualBox

    cd 进入目录:/etc/yum.repos.d 新建一个文件virtualbox.repo, 输入如下内容: [virtualbox] name=Oracle Linux / RHEL / Cent ...

  8. 哈希hash

    定义 是把任意长度的输入(又叫做预映射pre-image)通过散列算法变换成固定长度的输出,该输出就是散列值 生成方法 hash() 哈希特性 不可逆 :在具备编码功能的同时,哈希算法也作为一种加密算 ...

  9. Javascript高级面试

    原型 异步 一.什么是单线程,和异步有什么关系 单线程:只有一个线程,同一时间只能做一件事原因:避免DOM渲染的冲突解决方案:异步 为什么js只有一个线程:避免DOM渲染冲突 浏览器需要渲染DOM J ...

  10. 细说C#的ReferenceEquals,Equals和==比较运算符

    C# 中有两种不同的相等:引用相等和值相等.值相等是大家普遍理解的意义上的相等:它意味着两个对象包含相同的值.例如,两个值为 2 的整数具有值相等性.引用相等意味着要比较的不是两个对象,而是两个对象引 ...