HDU ACM 1515 Anagrams by Stack
Anagrams by Stack
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 870 Accepted Submission(s): 419
[
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.
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.
[
]
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.
adamm
bahama
bahama
long
short
eric
rice
【随笔】比较有意思的一题水题,题目的意思是说给你两串字符串,一串是初始的串,一串为目的串,你要做的就是通过栈的先进先出的方式将这初始的字符串一个一个字符放进或推出,使最后出来的字符串是目的串。解题的话也就是用搜索的暴力的方法一个一个放进去,这里直接调用stack容器使过程满足栈的运行机制
【PS】每个输出的io后面都跟有空格,而且每次当前出来的字符的字符串都要跟目的字符串进行比较以减少不必要的情况,减小耗时
#include<cstdio>
#include<cstring>
#include<cmath>
#include<stack>
#include<algorithm>
#include<stdlib.h>
#define SIZE 1000 using namespace std;
char res[SIZE];
char input[SIZE];
char elem[SIZE];
int opera[SIZE];
int len;
stack<char>sample; void Traverse(int elen, int rlen, int oplen)
{
if(rlen == len)
{
bool flag = true;
for(int i=; i<len; ++i)
if(res[i] != input[i])
{
flag = false;
break;
}
if(flag)
{
for(int i=; i<oplen; ++i)
printf("%c ", opera[i]);
printf("\n");
}
else return;
}
bool flag = true;
for(int i=; i<rlen; ++i)
if(res[i] != input[i])
{
flag = false;
break;
}
if(!flag) return; if(elen<len)
{
sample.push(elem[elen]);
opera[oplen] = 'i';
Traverse(elen+, rlen, oplen+);
sample.pop();
}
if(!sample.empty())
{
res[rlen] = sample.top();
sample.pop();
opera[oplen] = 'o';
Traverse(elen, rlen+, oplen+);
sample.push(res[rlen]);
}
return;
} int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
#endif
while(scanf("%s", elem) != EOF)
{
scanf("%s", input);
printf("[\n");
len = strlen(input);
if(len == strlen(elem))
Traverse(, , );
printf("]\n");
}
return ;
}
HDU ACM 1515 Anagrams by Stack的更多相关文章
- hdu 1515 Anagrams by Stack
题解: 第一:两个字符不相等(即栈顶字符与目标字符不相等):这种情况很容易处理,将匹配word的下一个字符入栈,指针向后挪已为继续递归. 第二:两个字符相等(即栈顶字符与目标字符相等):这种情况有两种 ...
- 【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+数据结构)
Anagrams by Stack 题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4 题目大意:输入两个字符串序列,判 ...
- ZOJ 1004 Anagrams by Stack
Anagrams by Stack 题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1004 题意:通过堆栈实现将一 ...
- hdu acm 1028 数字拆分Ignatius and the Princess III
Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...
- stack+DFS ZOJ 1004 Anagrams by Stack
题目传送门 /* stack 容器的应用: 要求字典序升序输出,所以先搜索入栈的 然后逐个判断是否满足答案,若不满足,回溯继续搜索,输出所有符合的结果 */ #include <cstdio&g ...
- 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 ...
- HDU ACM 题目分类
模拟题, 枚举1002 1004 1013 1015 1017 1020 1022 1029 1031 1033 1034 1035 1036 1037 1039 1042 1047 1048 104 ...
随机推荐
- 怎样在Ubuntu系统安装可用的QQ
http://jingyan.baidu.com/article/9f63fb91d1f6bbc8400f0e1c.html
- html5 audio音频播放全解析
序 html5开启了一个新时代,因为它让浏览器本身变得不那么被动,audio api就是一个典型的列子,在html5还没确定之前,如果想要在网页上听音乐看视频唯一的办法就是用flash意思是当你没有给 ...
- ubuntu10.04共享文件夹
ubuntu10.04共享文件夹 参考http://jingyan.baidu.com/album/9989c746084c70f648ecfe99.html,共享了home文件夹,然后把共享文件夹映 ...
- Entity Framework Architecture
http://www.entityframeworktutorial.net/EntityFramework-Architecture.aspx The following figure shows ...
- Android 第三方应用接入微信平台(1)
关键字:微信开放平台 Android第三方应用接入微信 微信平台开放后倒是挺火的,许多第三方应用都想试下接入微信这个平台, 毕竟可以利用微信建立起来的关系链来拓展自己的应用还是挺不错的,可 以节约 ...
- NPOI导出Excel表功能实现(多个工作簿)(备用)
Excel生成操作类: 代码 using System; using System.Collections.Generic; using System.Text; using System.IO; u ...
- .NET 实现异步处理的集中方式
对于异步,相信大家都不十分陌生.准确点来说就是方法执行后立即返回,待到执行完毕会进行通知.就是当一个任务在执行的时候,尤其是需要耗费很长的时间进行处理的任务,如果利用单线程进行操作的话,势必造成界面的 ...
- POI刷新数据后的函数(公式)更新问题
使用POI将Excel模板中的数据进行更新,这应该是很常见的操作 下面就贴上我的一小段代码 public class ModifyExcel { /** * @param fileName Excel ...
- [ionic开源项目教程] - 第9讲 新闻详情页的实现
目录 [ionic开源项目教程] 第1讲 前言,技术储备,环境搭建,常用命令 [ionic开源项目教程] 第2讲 新建项目,架构页面,配置app.js和controllers.js [ionic开源项 ...
- VS2015新功能
今天有幸参加了微软的 Visual Studio Dev Day,趁还没有忘记今天的学习内容. 先把这些内容记录下来,如果有其他人也参加此次交流活动,请补充完善. VS2015新功能 1,Roslyn ...