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. 学习笔记之Redis

    Redis https://redis.io/ redis.cn http://www.redis.cn/ Azure Redis Cache Documentation - Tutorials, A ...

  2. python selenium-6 HTML测试报告

    1.生成HTML测试报告 import unittest,sys from selenium import webdriver from time import sleep class TestBai ...

  3. appium 3-4-1034等待、日志、性能数据、xpath定位、web driver协议

    1.等待 1.1精确等待 sleep 不推荐 @Test public void testWait1() throws InterruptedException{ day_time(); Thread ...

  4. mysql binlog协议分析--具体event

    这几天在修改canal, 连接mysql和maria接收到的event有所区别 拿一个简单的insert sql来举例 mysql 会有以下几个event写入到binlog里 1.ANONYMOUS_ ...

  5. Linux 期中架构 rsync

    上篇 Rsync rysnc查看版本   --version 全量  与增量在效率上有区别 cp mv scp  --全量复制 rsync       --增量复制 rsync利用的是quick ch ...

  6. sklearn.svm.SVC 参数说明

    原文地址:sklearn.svm.SVC 参数说明 ============================== 资源: sklearn官网+DOC 库下载GitHub =============== ...

  7. Spring Security编程模型

    1.采用spring进行权限控制 url权限控制 method权限控制 实现:aop或者拦截器(本质就是之前之后进行控制)--------------------proxy就是 2.权限模型: 本质理 ...

  8. 处理存在UNASSIGNED的主分片导致Elasticsearch集群状态为Red的问题

    我们默认是开启了自动分配的,但仍然会因为服务器软硬件的原因导致分配分配失败,从而出现UNASSIGNED的分片,如果存在该状态的主分片则会导致集群为Red状态.此时我们可以通过reroute API进 ...

  9. vb 导出excel生成图表统计

    Private Sub btnExprot_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnE ...

  10. attack source code

    不废话,直接上代码, 先看截图use pictures;