个人心得:算法书中的第一个例题就来了一个下马威,虽然题意很好理解但是做起来确实这么不顺手,所以自己对于搜索和堆栈理解的并不是很好,

以前也是很多这样的题目无法实施,这题要做的很明确就是输出正确的能依靠栈完成字符串的变化,很明显答案很多所以必须搜索确定出栈的位置,

但是自己无法控制好搜索,题解很清晰,

个人收获:vector 可以用于搜索更方便,然后搜索的时候注意细节和base基准情况

题目:

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
]
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<stack>
#include<set>
#include<queue>
#include<algorithm>
using namespace std;
string a,b;
stack<char >build;
vector<char >operate;
int length;
void dfs(int ipush,int ipop){
if(ipush==length&&ipop==length)
{
for(int i=;i<operate.size();i++)
cout<<operate[i]<<" ";
cout<<endl;
}
if(ipush+<=length)
{
build.push(a[ipush]);
operate.push_back('i');
dfs(ipush+,ipop);
build.pop();
operate.pop_back();
}
if(ipop+<=ipush&&ipop+<=length&&build.top()==b[ipop]){
char tc=build.top();
build.pop();
operate.push_back('o');
dfs(ipush,ipop+);
build.push(tc);
operate.pop_back();
}
}
int main()
{
while(cin>>a>>b){
length=a.length();
cout<<"["<<endl;
dfs(,);
cout<<"]"<<endl;
}
return ;
}
												

ZOJ Anagrams by Stack(堆栈中的搜索)的更多相关文章

  1. poj 1363 Rails in PopPush City &&【求堆栈中合法出栈顺序次数】

    问题如下: 问题 B: Rails 时间限制: Sec 内存限制: MB 提交: 解决: [提交][状态][讨论版] 题目描述 There is a famous railway station in ...

  2. ZOJ 1004 Anagrams by Stack

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

  3. stack+DFS ZOJ 1004 Anagrams by Stack

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

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

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

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

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

  6. [ZJU 1004] Anagrams by Stack

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

  7. 如何改变Activity在当前任务堆栈中的顺序,Intent参数大全

    引用:http://blog.csdn.net/think_soft/article/details/7477072 本示例演示如何通过设置Intent对象的标记,来改变当前任务堆栈中既存的Activ ...

  8. HDU ACM 1515 Anagrams by Stack

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

  9. stack堆栈容器、queue队列容器和priority_queue优先队列容器(常用的方法对比与总结)

    stack堆栈是一个后进先出的线性表,插入和删除元素都在表的一端进行. stack堆栈的使用方法: 采用push()方法将元素入栈: 采用pop()方法将元素出栈: 采用top()方法访问栈顶元素: ...

随机推荐

  1. PAT 天梯赛 L1-040. 最佳情侣身高差 【水】

    题目链接 https://www.patest.cn/contests/gplt/L1-040 AC代码 #include <iostream> #include <cstdio&g ...

  2. Linux centos7 安装 keepalived-2.0.6

    1.下载(版本:2.0.6) cd /home/install/ wget http://124.205.69.170/files/1255000006EF2AA1/www.keepalived.or ...

  3. Shell 条件判断总结

    -b file 若文件存在且是一个块特殊文件,则为真 -c file 若文件存在且是一个字符特殊文件,则为真 -d file 若文件存在且是一个目录,则为真 -e file 若文件存在,则为真 -f ...

  4. 学会Retrofit+OkHttp+RxAndroid三剑客的使用,让自己紧跟Android潮流的步伐

    http://blog.csdn.net/iamzgx/article/details/51607387 概括 在上一篇博客android网络框架OkHttp之get请求(源码初识) 讲解了OkHtt ...

  5. 主攻ASP.NET MVC4.0之重生:Jquery Mobile 面板

    左滑动面板效果: 右滑动面板效果: @{ ViewBag.Title = "JQuery Mobile Web Page"; } <!DOCTYPE html> < ...

  6. 管道和xargs的区别

    1.概念 管道:将前一个命令的标准输出作为下一个命令的标准输入. xargs:将标准输入传递给下一个命令,作为其参数.(和管道连用)2.区别 2.区别 1:ls|cat是将ls的结果作为一个文件fil ...

  7. React Native的生命周期解析

    在React Native中使用组件来封装界面模块时,整个界面就是一个大的组件,开发过程就是不断优化和拆分界面组件.构造整个组件树的过程. 上张图涵盖了一个组件从创建.运行到销毁的整个过程.大家可以看 ...

  8. Android LCD(一):LCD基本原理【转】

    本文转载自:http://blog.csdn.net/longxiaowu/article/details/24787597 关键词:Android LCD TFT 液晶 偏光片 彩色滤光片  背光 ...

  9. [算法]在单链表和双链表中删除倒数第k个结点

    题目: 分别实现两个函数,一个可以删除单链表中倒数第K个节点,另一个可以删除双链表中倒数第K个节点. 要求: 如果链表长度为N,时间复杂度达到O(N),额外空间复杂度达到O(1). 解答: 让链表从头 ...

  10. freemarker模板解析过程

    例如:一个freemarker表达式<body> ${hello} </body>,会被解析成三个部分,分别是<body>${hello}</body> ...