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算法介绍 .总得来说是从某个状态开始,不断的转移状态知道无法转移,然后回到前一步的状态.如此不断的重复一直到找到最终的解.根据这个特点,常常会用到递归. ...
随机推荐
- 第二次程序+PSP0级
第二周,老师接着上次的程序有对四则运算的程序,做出来一些要求,这次要求可以控制乘除法,有无括号,控制输出方式,控制结果有无负数,有无余数. 我在对原先的程序分析了一下,发现我原先的程序可扩展性特别差, ...
- ASP.NET存储Session的StateServer
由于公司要对服务器做个负载均衡,所以Web项目在两台前端服务器(web1.web2)各部署了一份.但是在项目中会用到session.当一开始在web1上登陆后,由于web1之后负载可能会变大,就有可能 ...
- babel-preset-env: a preset that configures Babel for you
转载 babel-preset-env is a new preset that lets you specify an environment and automatically enables t ...
- 第180天:HTML5——本地存储
本地存储 客户端存储数据的方法 cookie 方法 localStorage方法 sessionStorage方法 一.localStorage 1.存储特点: localStorage方法存储的数据 ...
- 【EF】Entity Framework Core 命名约定
本文翻译自<Entity Framework Core: Naming Convention>,由于水平有限,故无法保证翻译完全正确,欢迎指出错误.谢谢! 注意:我使用的是 Entity ...
- 【JavaScript&jQuery】省市区三级联动
HTML: <%@page import="com.mysql.jdbc.Connection"%> <%@ page language="java&q ...
- 【开发工具IDE】eclipse的web项目的tomcat安装部署问题
一.发现问题 在eclipse中新建Dynamic Web Project,配置好本地的tomcat并写好代码后选择Run on Server,但运行后发现在tomcat的安装目录下的webapps并 ...
- 关于slow http attack以及apche tomcat的应对方式
HTTP 的 Slow Attack 有着悠久历史的 HTTP DOS 攻击方式,最早大约追溯到 5 年前,按理说早该修复了,但是 Apache 的默认配置中仍然没有添加相关配置,或者他们认为这是 f ...
- QString::QString 中文乱码
QString::QString 中文乱码 处理方法: 1. QString str = QString::fromLocal8Bit("中文"); // vs2008 vs200 ...
- hive 导入数据
1.load data load data local inpath "/home/hadoop/userinfo.txt" into table userinfo; " ...