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 ...
随机推荐
- [POJ1631]Bridging signals (DP,二分优化)
题目链接:http://poj.org/problem?id=1631 就是求一个LIS,但是范围太大(n≤40000),无法用常规O(n²)的朴素DP算法,这时需要优化. 新加一个数组s[]来维护长 ...
- 《OD大数据实战》MongoDB环境搭建
一.MongonDB环境搭建 1. 下载 https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.0.6.tgz 2. 解压 tar -zxvf ...
- 《OD学算法》排序
参考 http://www.cnblogs.com/kkun/archive/2011/11/23/2260312.html http://blog.csdn.net/wuxinyicomeon/ar ...
- 利用RunTime解决由NSTimer导致的内存泄漏
NSTimer使用场景 用NSTimer来实现每隔一定时间执行制定的任务,例如最常见的广告轮播图,使用NSTimer实现这个功能很简单代码如下 NSTimer *_timer; _timer = [N ...
- Codeforces 443 B Kolya and Tandem Repeat【暴力】
题意:给出一个字符串,给出k,可以向该字符串尾部添加k个字符串,求最长的连续重复两次的子串 没有想出来= =不知道最后添加的那k个字符应该怎么处理 后来看了题解,可以先把这k个字符填成'*',再暴力枚 ...
- 在view中常见的四种方法的使用场合
四种方法,使view创建好里面就有东西:[1.init 2.initWithFrame使用代码创建的时候.(从文件创建的时候不一定调用:1.init 2.initWithFrame这两个方法) 3 ...
- BZOJ 4571 美味
又一部SCOI血泪史.... 唉. 就是在这棵树上一遍又一遍跑嘛. 以后不要直接求答案啊.要最后再异或起来. 要学习简单的代码风格. #include<iostream> #include ...
- js解决快速回车重复订单提交(客户端方式)
Html代码: <form action="order_add_data.php" method="post" name="order_adds ...
- HDU2028 Lowest Common Multiple Plus
解题思路:最近很忙,有点乱,感觉对不起自己的中国好队友. 好好调整,一切都不是问题,Just do it ! 代码: #include<cstdio> int gcd(int a, i ...
- QR二维码(转)
二维码又称QR Code,QR全称Quick Response,是一个近几年来移动设备上超流行的一种编码方式,它比传统的Bar Code条形码能存更多的信息,也能表示更多的数据类型:比如:字符,数字, ...