ZOJ Anagrams by Stack(堆栈中的搜索)
个人心得:算法书中的第一个例题就来了一个下马威,虽然题意很好理解但是做起来确实这么不顺手,所以自己对于搜索和堆栈理解的并不是很好,
以前也是很多这样的题目无法实施,这题要做的很明确就是输出正确的能依靠栈完成字符串的变化,很明显答案很多所以必须搜索确定出栈的位置,
但是自己无法控制好搜索,题解很清晰,
个人收获: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(堆栈中的搜索)的更多相关文章
- poj 1363 Rails in PopPush City &&【求堆栈中合法出栈顺序次数】
		问题如下: 问题 B: Rails 时间限制: Sec 内存限制: MB 提交: 解决: [提交][状态][讨论版] 题目描述 There is a famous railway station in ... 
- ZOJ 1004 Anagrams by Stack
		Anagrams by Stack 题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1004 题意:通过堆栈实现将一 ... 
- stack+DFS ZOJ 1004 Anagrams by Stack
		题目传送门 /* stack 容器的应用: 要求字典序升序输出,所以先搜索入栈的 然后逐个判断是否满足答案,若不满足,回溯继续搜索,输出所有符合的结果 */ #include <cstdio&g ... 
- Anagrams by Stack(深度优先搜索)
		ZOJ Problem Set - 1004 Anagrams by Stack Time Limit: 2 Seconds Memory Limit: 65536 KB How can a ... 
- ZOJ 1004 Anagrams by Stack(DFS+数据结构)
		Anagrams by Stack 题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4 题目大意:输入两个字符串序列,判 ... 
- [ZJU 1004] Anagrams by Stack
		ZOJ Problem Set - 1004 Anagrams by Stack Time Limit: 2 Seconds Memory Limit: 65536 KB How can a ... 
- 如何改变Activity在当前任务堆栈中的顺序,Intent参数大全
		引用:http://blog.csdn.net/think_soft/article/details/7477072 本示例演示如何通过设置Intent对象的标记,来改变当前任务堆栈中既存的Activ ... 
- HDU ACM 1515 Anagrams by Stack
		Anagrams by Stack Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ... 
- stack堆栈容器、queue队列容器和priority_queue优先队列容器(常用的方法对比与总结)
		stack堆栈是一个后进先出的线性表,插入和删除元素都在表的一端进行. stack堆栈的使用方法: 采用push()方法将元素入栈: 采用pop()方法将元素出栈: 采用top()方法访问栈顶元素: ... 
随机推荐
- Android中的动画使用总结
			android中动画可分为三种:帧动画,补间动画,和属性动画.其中属性动画是google推荐的,它可以实现前面两种动画的效果,运用起来更加灵活. 帧动画:顾名思义,就是一帧一帧的图片,快速播放形成的动 ... 
- SourceTree的基本使用---基本介绍/本地开发
			转载自https://www.cnblogs.com/tian-xie/p/6264104.html 1. SourceTree是什么 拥有可视化界面的项目版本控制软件,适用于git项目管理 wind ... 
- IDEA中文出现乱码解决
			转自:http://lcl088005.iteye.com/blog/2284696 我是个idea的忠实用户,新公司的项目都是用eclipse做的,通过svn拉下代码后发现,注释的内容里,中文内容都 ... 
- Kafka Confluent
			今天我们要讲的大数据公司叫作Confluent,这个公司是前LinkedIn员工出来后联合创办的,而创业的基础是一款叫作Apache Kafka的开源软件. Confluen联合创始人Jun Rao即 ... 
- third application :Directions widget
			<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ... 
- JS字符串数组转换
			字符串转数组: str.split(';') 数组转字符串: arr.join(';') 
- Linux软件包管理  RMP包管理
			概述 RPM 包的命名一般都会遵守统一的命名规则,例如: httpd-2.2.15-15.el6.centos.1.i686.rpm 其中的各项代表的含义如下: httpd:软件包名. 2.2.15: ... 
- Hearbeat + Nginx 安装配置
			Hearbeat + Nginx 安装配置 实验环境 两台主机:Linux Centos 6.5 32位 主 服务端:Hearbeat + Nginx eth0:192.168.1.160(公网) e ... 
- 三、golang时间、流程控、函数
			一.本篇内容 1.string和strconv使用 2.go中的时间和日期类型 3.流程控制 4.函数讲解 二.string和strconv使用 1. string.HasPrefix(s trin ... 
- Cocos2d-x项目移植到WP8系列之七:中文显示乱码
			原文链接:http://www.cnblogs.com/zouzf/p/3984628.html C++和C#互调时经常会带一些参数过去例如最常见的字符串,如果字符串里有中文的话,会发现传递过去后变成 ... 
