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 oproduce 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
]
深度优先搜索即可。注意输出格式,每个i或者o后面都有空格
#include <iostream>
#include <stack>
#include <cstdio>
#include <cstring> using namespace std; const int MAX_LEN = ;
char s[MAX_LEN], d[MAX_LEN], ans[MAX_LEN];
stack<char> sta; void DFS(int si, int di, int ansi) //三个参数分别是s,d,ans的下标
{
if(s[si] == '\0')
{
if(di == si)
{
for(int i = ; i < ansi; i++)
printf("%c ", ans[i]);
puts("");
return ;
}
else
{
if(!sta.empty() && sta.top() == d[di])
{
char t = sta.top();
sta.pop();
ans[ansi] = 'o';
DFS(si, di + , ansi + );
sta.push(t);
return ;
}
else return ;
}
}
sta.push(s[si]);
ans[ansi] = 'i';
DFS(si + , di, ansi + );
if(!sta.empty()) sta.pop();
if(!sta.empty() && sta.top() == d[di])
{
char t = sta.top();
sta.pop();
ans[ansi] = 'o';
DFS(si, di + , ansi + );
sta.push(t);
return ;
}
} int main()
{
while(scanf("%s %s", s, d) != EOF)
{
int lens = strlen(s);
int lend = strlen(d);
if(lens != lend)
{
printf("[\n]\n");
}
else
{
while(!sta.empty()) sta.pop();
printf("[\n");
DFS(, , );
printf("]\n");
}
}
return ;
}
Anagrams by Stack(深度优先搜索)的更多相关文章
- [ZOJ 1004] Anagrams by Stack (简单搜索)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1004 题目大意:给你个栈,给你源串和目标串,按字典序输出符合要求 ...
- castle problem——(深度优先搜索,递归实现和stack实现)
将问题的各状态之间的转移关系描述为一个图,则深度优先搜索遍历整个图的框架为:Dfs(v) {if( v 访问过)return;将v标记为访问过;对和v相邻的每个点u: Dfs(u);}int main ...
- 【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 题意:通过堆栈实现将一 ...
- 深度优先搜索(DFS)
定义: (维基百科:https://en.wikipedia.org/wiki/Depth-first_search) 深度优先搜索算法(Depth-First-Search),是搜索算法的一种.是沿 ...
- 图的遍历之深度优先搜索(DFS)
深度优先搜索(depth-first search)是对先序遍历(preorder traversal)的推广.”深度优先搜索“,顾名思义就是尽可能深的搜索一个图.想象你是身处一个迷宫的入口,迷宫中的 ...
- stack+DFS ZOJ 1004 Anagrams by Stack
题目传送门 /* stack 容器的应用: 要求字典序升序输出,所以先搜索入栈的 然后逐个判断是否满足答案,若不满足,回溯继续搜索,输出所有符合的结果 */ #include <cstdio&g ...
- HDU ACM 1515 Anagrams by Stack
Anagrams by Stack Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- 深度优先搜索(DFS)——部分和问题
对于深度优先搜索,这里有篇写的不错的博客:DFS算法介绍 .总得来说是从某个状态开始,不断的转移状态知道无法转移,然后回到前一步的状态.如此不断的重复一直到找到最终的解.根据这个特点,常常会用到递归. ...
随机推荐
- 对其中的一个特点将NABC的分析结果
一.题目要求 每一个组员针对其中的一个特点将NABC的分析结果发表博客上(截止日期4月8日晚24:00前). 二.分析结果 特点之一:通讯方便 <渴了么>这个安卓APP特点之一就是通讯方便 ...
- python 动态获取当前运行的类名和函数名的方法
一.使用内置方法和修饰器方法获取类名.函数名 python中获取函数名的情况分为内部.外部,从外部的情况好获取,使用指向函数的对象,然后用__name__属性 复制代码代码如下: def a():pa ...
- ACM ICPC 2016–2017, NEERC, Northern Subregional Contest Problem J. Java2016
题目来源:http://codeforces.com/group/aUVPeyEnI2/contest/229510 时间限制:2s 空间限制:256MB 题目大意: 给定一个数字c 用 " ...
- Java中final修饰符深入研究
一.开篇 本博客来自:http://www.cnblogs.com/yuananyun/ final修饰符是Java中比较简单常用的修饰符,同时也是一个被"误解"较多的修饰符.对很 ...
- Java实现的词频统计——单元测试
前言:本次测试过程中发现了几个未知字符,这里将其转化为十六进制码对其加以区分. 1)保存统计结果的Result文件中显示如图: 2)将其复制到eclipse环境下的切分方法StringTokenize ...
- ASP.NET存储Session的StateServer
由于公司要对服务器做个负载均衡,所以Web项目在两台前端服务器(web1.web2)各部署了一份.但是在项目中会用到session.当一开始在web1上登陆后,由于web1之后负载可能会变大,就有可能 ...
- crontab & php实现多进程思路
<?php $startTime = time(); while(1) { if (time() - $startTime > 600) { exit; } // ... Do SomeT ...
- gulp 定义依赖关系
var gulp = require('gulp'); // 返回一个 callback,因此系统可以知道它什么时候完成 gulp.task('one', function(cb) { // 做一些事 ...
- UVA11625_Lines of Containers
题意很简单,给你一个n*m的矩阵,现在问你这个矩阵能否变为标准矩阵(即数字从小到大),如果能最少需要几步呢? 其实是个赤果果的水题.记得暑假安叔也出过一个类似的题目,那个好像是在codeforces上 ...
- Collection接口框架
1. Collection接口 其主要的UML类图: Collection接口继承自Iterable接口.Iterable接口中定义了Iterable方法,该方法会返回一个迭代器,用于遍历合集中的元素 ...