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. python学习中

    python中的单引号.双引号.三引号的用法 网上也查找了资料,理解的都有些费劲 就自己验证了一下(主要是目前掌握的python知识,不知道什么时候会同时用到这三种引号) 用python3验证的 单引 ...

  2. 【HANA系列】SAP HANA ODBC error due to mismatch of version

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[HANA系列]SAP HANA ODBC er ...

  3. C#得到10000以内素数

    偶数除了二都不是素数 一个数 n 如果是合数,那么它的所有的因子不超过sqrt(n)--n的开方 int i, j, n = 10000; for (i = 3; i <= n; i += 2) ...

  4. 【组策略】1.组策略介绍group policy

    组策略介绍group policy 高效学习法,念念不忘,必有回响. 分享一个高效学习思维,潜意识思考.就是在您没有大量时间的情况下,学习十分钟. 然后离开去完成别的事情的时候,大脑潜意识中还会继续思 ...

  5. ---Mock---基本使用

    一.mock解决的问题 开发时,后端还没完成数据输出,前端只好写静态模拟数据.数据太长了,将数据写在js文件里,完成后挨个改url.某些逻辑复杂的代码,加入或去除模拟数据时得小心翼翼.想要尽可能还原真 ...

  6. 20191127 Spring Boot官方文档学习(4.10)

    4.10.使用SQL数据库 从使用JdbcTemplate直接的JDBC访问到完整的"对象关系映射"技术(例如Hibernate),Spring框架为使用SQL数据库提供了广泛的支 ...

  7. 任务调度之 Quartz

    任务调度的背景 在业务系统中有很多这样的场景: 账单日或者还款日上午 10 点,给每个信用卡客户发送账单通知,还款通知.如何判断客户的账单日.还款日,完成通知的发送? 银行业务系统,夜间要完成跑批的一 ...

  8. Spark-Core RDD转换算子-kv型

    大多数的 Spark 操作可以用在任意类型的 RDD 上, 但是有一些比较特殊的操作只能用在key-value类型的 RDD 上. 这些特殊操作大多都涉及到 shuffle 操作, 比如: 按照 ke ...

  9. webpack e6转化成es5 配置方法

    方法一: https://www.babeljs.cn/setup#installation 按照babel官方的配置配 方法二: https://www.jianshu.com/p/ce28cedd ...

  10. easy-mock的运用

    一.概念 Easy Mock 是杭州大搜车无线团队出品的一个极其简单.高效.​可视化.并且能快速生成模拟数据的 在线 mock 服务 .以项目管理的方式组织 Mock List,能帮助我们更好的管理 ...