zoj1004-Anagrams by Stack 【栈 dfs】
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】的更多相关文章
- ZOJ 1004 Anagrams by Stack(DFS+数据结构)
Anagrams by Stack 题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4 题目大意:输入两个字符串序列,判 ...
- ZOJ1004 Anagrams by Stack
题目大意:规定 i 为入栈,o 为出栈,现在给两个字符串st1,st2,现在要将st1转化为st2,转化方法是,st1中字符从头开始入栈,并合理出栈构造出st2.请输出所有可能的出入栈步骤. 深度优先 ...
- stack+DFS ZOJ 1004 Anagrams by Stack
题目传送门 /* stack 容器的应用: 要求字典序升序输出,所以先搜索入栈的 然后逐个判断是否满足答案,若不满足,回溯继续搜索,输出所有符合的结果 */ #include <cstdio&g ...
- 【Acm】算法之美—Anagrams by Stack
题目概述:Anagrams by Stack How can anagrams result from sequences of stack operations? There are two seq ...
- ZOJ 1004 Anagrams by Stack
Anagrams by Stack 题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1004 题意:通过堆栈实现将一 ...
- HDU ACM 1515 Anagrams by Stack
Anagrams by Stack Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- Anagrams by Stack(深度优先搜索)
ZOJ Problem Set - 1004 Anagrams by Stack Time Limit: 2 Seconds Memory Limit: 65536 KB How can a ...
- [ZJU 1004] Anagrams by Stack
ZOJ Problem Set - 1004 Anagrams by Stack Time Limit: 2 Seconds Memory Limit: 65536 KB How can a ...
- java - Stack栈和Heap堆的区别
首先分清楚Stack,Heap的中文翻译:Stack—栈,Heap—堆. 在中文里,Stack可以翻译为“堆栈”,所以我直接查找了计算机术语里面堆和栈开头的词语: 堆存储 ...
随机推荐
- java web 程序---投票系统
1.这里会连接数据库--JDBC的学习实例 一共有3个页面. 2.第一个页面是一个form表单,第二个页面是处理数据,第三个页面是显示页面 vote.jsp <body bgcolor=&quo ...
- ES之八:elasticsearch2.x下的JAVA API示例
D:\soft\elasticsearch\elasticsearch-2.1.0\lib package com.dxz.es; import java.net.InetAddress; impor ...
- [转]SendKeys.Send 方法
SendKeys.Send 方法 向活动应用程序发送击键. 转载自: https://msdn.microsoft.com/zh-cn/library/system.windows.forms.sen ...
- 利用.pbk来实现ADSL开机自动拨号
当你新建拨号连接或者VPN连接之后在你的电脑里会创建一个.pbk的文件 这个.pbk的文件可以说是一个集合,将你电脑的所有连接都保存在一起. 同时你还可以将此连接复制起来传给其他人. 系统默认的.pb ...
- VMware中,该如何理解桥接网络与NAT 网络模式
原创 2016年11月16日 23:26:34,原文地址如下: http://blog.csdn.net/u010801439/article/details/53193113 首先,我在VMware ...
- python之socket编写
Socket 类型 套接字格式: socket(family,type[,protocal]) 使用给定的地址族.套接字类型.协议编号(默认为0)来创建套接字. socket类型 描述 socket. ...
- 解决QT出现XXXX.dll不能加载问题
第一步:下载相关动态链接文件(这里以ig4icd32.dll为例子) 下载地址:ig4icd32.dll文件 第二步:把下载的文件放在两个地方,记住!一定得放在两个地方,我试了少一个都不行! C:\W ...
- 为挂载到/home的RAID磁盘组扩容
公司一台DELL服务器,安装的Ubuntu16.04系统,原来是6块1.2T的SAS盘做RAID-5挂载到/home,现在/home空间不够用了,需要扩容,再增加2块1.2T的盘.整个操作不复杂,但有 ...
- 23. oralce11g导出dmp然后导入Oracle10g
解决方法: 一.在11g服务器上,使用expdp命令备份数据 EXPDP USERID='fjgs/fjgs@orcl' schemas=fjgs directory=DATA_PUMP_DIR du ...
- spring mvc 解决json 不能转换的问题
在要转的实体上加一个 @JsonIgnoreProperties(value = { "hibernateLazyInitializer", "handler" ...