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 ...
随机推荐
- DataGridView 相关操作
一.单元格内容的操作// 取得当前单元格内容 Console.WriteLine(DataGridView1.CurrentCell.Value); // 取得当前单元格的列 Index Consol ...
- 【笨嘴拙舌WINDOWS】GDI对象之位图
GDI对象在windows中可以描述成为一次绘画操作时可使用的抽象工具.包括(画笔.画刷.字体.区域.调色板.位图等) GDI对象里的对象概念和在编程领域中面向对象编程的对象概念是不一样的! GDI对 ...
- jQuery1.9+中删除了live以后的替代方法
.live() removed The .live() method has been deprecated since jQuery 1.7 and has been removed in 1.9. ...
- 51nod1406 与查询
这题卡I/O...dp一下... #include<cstdio> #include<cstring> #include<cctype> #include<a ...
- 总结css兼容问题
目前主流浏览器的兼容性做的都比较好了,本文主要针对IE6,7的不兼容问题进行解决. 1.有浮动存在时,计算一定要精确,不要让内容的宽高超出我们所设置的宽高,IE6下,内容会撑开设置好的高度. 解决方法 ...
- codevs 4919 线段树练习4
线段树水题.我是ziliuziliu,我是最强的#include<iostream> #include<cstdio> #include<cstring> #inc ...
- 【转】vim - tab变空格
vim中将tab自动转换成空格 在vim中,有时需要将tab转换成space.使用ret命令(replace tab).[range]ret[ab]! [new-tabstop] 举例:将第一行到文件 ...
- 【Python】类和对象、继承、使用文件、存储、异常、标准库(不懂)
当你调用这个对象的方法MyObject.method(arg1, arg2)的时候,这会由Python自动转为MyClass.method(MyObject, arg1, arg2)——这就是self ...
- mysql同步 小问题
由于历史遗留问题,我们的MySQL主从库的表结构不一致,主库的某个表tableA比从库表tableA少了一个字段. 当尝试在主库上更改表结构时,这行alter语句会随着binlog同步到从库,如果从库 ...
- jquery再学习(1)
一:jquery对象和js的dom对象相互转化 html代码 <ul> <li class="sxf" name="dd">第一< ...