[ZJU 1004] 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
]
Source: Zhejiang University Local Contest 2001
Solution:
暴力模拟
每次先搜索push,再判断能否pop。
回溯时需注意。
#include<bits/stdc++.h>
using namespace std;
char a[],b[];
int al;
int bl;
stack<char> T;
int opt[];
void DFS(int dep,int x,int y)
{
//cout << dep << ' ' << x << ' ' << y << endl;
if (x == al && y == bl)
{
//A successful opt.
for (int i=;i<=al+bl;i++)
{
if (opt[i] == )
cout << "i ";
else cout << "o ";
}
cout << endl;
return;
}
if (x != al)
{
opt[dep]=;
T.push(a[x]);
DFS(dep+,x+,y);
T.pop();
}
if (!T.empty() && y != bl && T.top() == b[y])
{
char tmp = T.top();
T.pop();
opt[dep]=;
DFS(dep+,x,y+);
T.push(tmp);
}
return;
}
void solve()
{
al = strlen(a);
bl = strlen(b);
while (!T.empty()) T.pop();
memset(opt,,sizeof(opt));
cout << '[' << endl;
DFS(,,);
cout << ']' << endl; }
int main()
{
while (cin >> a >> b)
solve();
}
[ZJU 1004] Anagrams by Stack的更多相关文章
- stack+DFS ZOJ 1004 Anagrams by Stack
题目传送门 /* stack 容器的应用: 要求字典序升序输出,所以先搜索入栈的 然后逐个判断是否满足答案,若不满足,回溯继续搜索,输出所有符合的结果 */ #include <cstdio&g ...
- ZOJ 1004 Anagrams by Stack
Anagrams by Stack 题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1004 题意:通过堆栈实现将一 ...
- ZOJ 1004 Anagrams by Stack(DFS+数据结构)
Anagrams by Stack 题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4 题目大意:输入两个字符串序列,判 ...
- [ZOJ 1004] Anagrams by Stack (简单搜索)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1004 题目大意:给你个栈,给你源串和目标串,按字典序输出符合要求 ...
- 1004 Anagrams by Stack
考察DFS的应用,用栈描述字符串的变化过程. #include <stdio.h> #include <string.h> int len1,len2; ],str2[],st ...
- Anagrams by Stack(深度优先搜索)
ZOJ Problem Set - 1004 Anagrams by Stack Time Limit: 2 Seconds Memory Limit: 65536 KB How can a ...
- HDU ACM 1515 Anagrams by Stack
Anagrams by Stack Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- 【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
细节问题各种虐!! 其实就是简单的一个深搜 看成二叉树来理解:每个节点有两个枝:入栈和出栈. 剪枝操作:只有当栈顶元素和当前位置的目标字符相同时才出栈,否则就不出栈 dfs写三个参数:depth搜索深 ...
随机推荐
- Tensorflow模型保存与载入
import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data #载入数据集 mnist = in ...
- 扫描 + 注解完成bean的自动配置
链接:https://pan.baidu.com/s/1W3TINXNnqpxmkIADOcJZCQ 提取码:fmt5 我们知道,我们一般是通过id或name调用getBean方法来从IOC容器中获取 ...
- C++智能指针 原理、使用与实现
目录 理解智能指针的原理 智能指针的使用 智能指针的设计和实现 1.智能指针的作用 C++程序设计中使用堆内存是非常频繁的操作,堆内存的申请和释放都由程序员自己管理.程序员自己管理堆内存可以提高了程序 ...
- Spring Task 任务调度(定时器)
1.任务调度SpringTask 1.1什么是任务调度 在企业级应用中,经常会制定一些“计划任务”,即在某个时间点做某件事情,核心是以时间为关注点,即在一个特定的时间点,系统执行指定的一个操作.常见的 ...
- while循环和字符串格式化
小知识点 \n#换行 \t #制表 \r #回车 print(a,b,c,d,sep="\n")换行 sep默认空格 1.while--关键字(死循环) while 空格 条件: ...
- (Windows)Python第三方库手动安装教程(以lxml库为例)
案例前提:已安装Python 已安装pip 1.进入官网https://www.lfd.uci.edu/~gohlke/pythonlibs/,搜索lxml库,下载到本地(放到Python目录下的Sc ...
- 通过编写串口助手工具学习MFC过程——(十)UpdateData()用法和编辑框的赋值、取值
通过编写串口助手工具学习MFC过程 因为以前也做过几次MFC的编程,每次都是项目完成时,MFC基本操作清楚了,但是过好长时间不再接触MFC的项目,再次做MFC的项目时,又要从头开始熟悉.这次通过做一个 ...
- 计算机系统结构总结_Instruction Set Architecture
Textbook:<计算机组成与设计——硬件/软件接口> HI<计算机体系结构——量化研究方法> QR 这节我们来看CPU内部的一些东西. Instruct ...
- 封装class类--分割类名后
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- .net 敏捷开发框架7.0.3 旗舰版
联系QQ:1516462411 索取