http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4

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
] 题意:由上一行单词通过栈改变为下一行单词,按字典序输出所有方法。
思路:栈+dfs回溯
代码:
 #include <fstream>
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib> using namespace std; #define EPS 1e-6
#define ll long long
#define INF 0x7fffffff const int N=; char s1[N],s2[N],r1[*N],sta[*N];
int l1,l2,rl,top; void Dfs(int ip,int op);//ip表示入栈次数,op表示出栈次数 int main()
{
//freopen("D:\\input.in","r",stdin);
while(~scanf("%s %s",s1,s2)){
puts("[");
l1=strlen(s1);
l2=strlen(s2);
rl=top=-;
top=;
if(l1==l2) Dfs(,);
puts("]");
}
return ;
}
void Dfs(int ip,int op){
if(ip==l1&&op==l2){
for(int i=;i<=rl;i++){
printf("%c ",r1[i]);
}
puts("");
}
if(ip<l1){
sta[++top]=s1[ip];
r1[++rl]='i';
Dfs(ip+,op);
top--;
rl--;
}
if(top>=&&sta[top]==s2[op]){
top--;
r1[++rl]='o';
Dfs(ip,op+);
sta[++top]=s2[op];
rl--;
}
}

zoj1004-Anagrams by Stack 【栈 dfs】的更多相关文章

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

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

  2. ZOJ1004 Anagrams by Stack

    题目大意:规定 i 为入栈,o 为出栈,现在给两个字符串st1,st2,现在要将st1转化为st2,转化方法是,st1中字符从头开始入栈,并合理出栈构造出st2.请输出所有可能的出入栈步骤. 深度优先 ...

  3. stack+DFS ZOJ 1004 Anagrams by Stack

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

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

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

  5. ZOJ 1004 Anagrams by Stack

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

  6. HDU ACM 1515 Anagrams by Stack

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

  7. Anagrams by Stack(深度优先搜索)

    ZOJ Problem Set - 1004 Anagrams by Stack Time Limit: 2 Seconds      Memory Limit: 65536 KB How can a ...

  8. [ZJU 1004] Anagrams by Stack

    ZOJ Problem Set - 1004 Anagrams by Stack Time Limit: 2 Seconds      Memory Limit: 65536 KB How can a ...

  9. java - Stack栈和Heap堆的区别

    首先分清楚Stack,Heap的中文翻译:Stack—栈,Heap—堆.         在中文里,Stack可以翻译为“堆栈”,所以我直接查找了计算机术语里面堆和栈开头的词语:        堆存储 ...

随机推荐

  1. JS URL 使用base64加密与解密和MD5解密

    JS编码方式: <script type="text/javascript"> document.write(encodeURI("http://www.w3 ...

  2. 操作系统-百科:Kylin (中国自主知识产权操作系统)

    ylbtech-操作系统-百科:Kylin (中国自主知识产权操作系统) Kylin操作系统是国家高技术研究发展计划(863计划)的重大成果之一,是以国防科技大学为主导,与中软.联想等单位联合设计和开 ...

  3. appium 3-31626 toast识别

    1.toast弹窗,普通方式不能获取 例如使用getPageSource是无法找到toast的信息,uiautomatorViewer加载页面时间较长,也很难采集到toast信息 2.通过curl命令 ...

  4. 浅析HttpCient

    HTTP 协议可能是现在 Internet 上使用得最多.最重要的协议了,越来越多的 Java 应用程序需要直接通过 HTTP 协议来访问网络资源.虽然在 JDK 的 java.net 包中已经提供了 ...

  5. 激活函数sigmoid、tanh、relu、Swish

    激活函数的作用主要是引入非线性因素,解决线性模型表达能力不足的缺陷 sigmoid函数可以从图像中看出,当x向两端走的时候,y值越来越接近1和-1,这种现象称为饱和,饱和意味着当x=100和x=100 ...

  6. 关于Oracle与MySQL的使用总结

    平时使用的比较多的数据库管理系统就是Oracle和MySQL,我在这里记录下使用过程中的遇到的问题以及解决方案,以备不时之需 Oracle 关于表空间 Oracle创建数据的代价还是比较大的,所以使用 ...

  7. C# 获取物理网卡Mac地址

    // <summary> /// 获取网卡物理地址 /// </summary> /// <returns></returns> public stat ...

  8. ubuntu 命令汇总

    1.linux添加全局变量 //查看当前的全局变量 echo $PATH //打开bashrc 文件,在最后面添加需要 加入的目录路径 vi ~/.bashrc //例如: export PATH=$ ...

  9. 搭建Discuz论坛

    准备 LAMP 环境 LAMP 是 Linux.Apache.MySQL 和 PHP 的缩写,是 Discuz 论坛系统依赖的基础运行环境.我们先来准备 LAMP 环境 安装 MySQL 使用 yum ...

  10. File处理

    package com.cfets.ts.u.shchgateway.util; import java.io.BufferedInputStream; import java.io.Buffered ...