[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搜索深 ...
随机推荐
- 【FICO系列】SAP ABAP&FI FI/CO接口:待更新的不一致的FI/CO凭证标题数据
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[FICO系列]SAP ABAP&FI ...
- 用VBA写一个计算器
着急的 玩家 可以 跳过“============”部分 ======================================可以跳过的 部分 开始==================== ...
- ubutnu同时安装OpenCV2和OpenCV3及contrib
1.OpenCV2源码安装 安装依赖项 sudo apt-get install build-essential //build-essential是c语言的开发包,包含了gcc make gdb和l ...
- 【Qt开发】V4L2 API详解 Buffer的准备和数据读取
前面主要介绍的是:V4L2 的一些设置接口,如亮度,饱和度,曝光时间,帧数,增益,白平衡等.今天看看V4L2 得到数据的几个关键ioctl,Buffer的申请和数据的抓取. 1. 初始化 Memory ...
- 【Qt开发】QT样式表单 qss的样式优化
QT样式表单 QT的样式表单允许我们在对程序不做任何代码上的更改的情况下轻松改变应用程序的外观. 其思想来源于网页设计中的CSS,即可以将功能设计和美学设计分开. 它的语法和概念和HTML CSS也是 ...
- 安装Git并关联
下载git 打开git bash 生成key将 key绑定到帐号 输入命令 ssh-keygen -t rsa -C 'LoginName' 根据命令生成的地址找到对应文件复制密钥 打开github登 ...
- django F和Q 关键字使用
F 的使用: 想给表里每个价格加上一百就要用上F,直接加是不行的.
- SSIS包定时执行
企业管理器 --管理 --SQL Server代理 --右键作业 --新建作业 --"常规"项中输入作业名称 --"步骤"项 --新建 --"步骤名& ...
- springboot 配置
springboot 配置文件中属性变量引用方式@@解析 这种属性应用方式是field_name=@field_value@. 两个@符号是springboot为替代${}属性占位符产生,原因是${} ...
- linux查看端口是否被占用
1.使用lsof lsof -i:端口号查看某个端口是否被占用 2.使用netstat 使用netstat -anp|grep 80