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

Problem Description
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.

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.

 
 
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.

 
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
]
 
Source
 
Recommend
LL

【随笔】比较有意思的一题水题,题目的意思是说给你两串字符串,一串是初始的串,一串为目的串,你要做的就是通过栈的先进先出的方式将这初始的字符串一个一个字符放进或推出,使最后出来的字符串是目的串。解题的话也就是用搜索的暴力的方法一个一个放进去,这里直接调用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的更多相关文章

  1. hdu 1515 Anagrams by Stack

    题解: 第一:两个字符不相等(即栈顶字符与目标字符不相等):这种情况很容易处理,将匹配word的下一个字符入栈,指针向后挪已为继续递归. 第二:两个字符相等(即栈顶字符与目标字符相等):这种情况有两种 ...

  2. 【Acm】算法之美—Anagrams by Stack

    题目概述:Anagrams by Stack How can anagrams result from sequences of stack operations? There are two seq ...

  3. ZOJ 1004 Anagrams by Stack(DFS+数据结构)

    Anagrams by Stack 题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4 题目大意:输入两个字符串序列,判 ...

  4. ZOJ 1004 Anagrams by Stack

    Anagrams by Stack 题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1004 题意:通过堆栈实现将一 ...

  5. 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 ...

  6. stack+DFS ZOJ 1004 Anagrams by Stack

    题目传送门 /* stack 容器的应用: 要求字典序升序输出,所以先搜索入栈的 然后逐个判断是否满足答案,若不满足,回溯继续搜索,输出所有符合的结果 */ #include <cstdio&g ...

  7. Anagrams by Stack(深度优先搜索)

    ZOJ Problem Set - 1004 Anagrams by Stack Time Limit: 2 Seconds      Memory Limit: 65536 KB How can a ...

  8. [ZJU 1004] Anagrams by Stack

    ZOJ Problem Set - 1004 Anagrams by Stack Time Limit: 2 Seconds      Memory Limit: 65536 KB How can a ...

  9. HDU ACM 题目分类

    模拟题, 枚举1002 1004 1013 1015 1017 1020 1022 1029 1031 1033 1034 1035 1036 1037 1039 1042 1047 1048 104 ...

随机推荐

  1. file的这几个取得path的方法各有不同,下边说说详细的区别

    html, body { font-size: 15px; } body { font-family: Helvetica, "Hiragino Sans GB", 微软雅黑, & ...

  2. Introduction

    http://www.entityframeworktutorial.net/EntityFramework5/entity-framework5-introduction.aspx Basics o ...

  3. 在AChartEngine上绘图,手指标记当前位置

    最近要做一个绘图项目,需要在ACE折线图上再绘出一条红标记当前坐标,经过这几天研究,可以给大家分享一下了.先上效果图吧! 代码里的注释还是比较清楚,就不作说明了. package com.exampl ...

  4. 《OD大数据实战》Hive环境搭建

    一.搭建hadoop环境 <OD大数据实战>hadoop伪分布式环境搭建 二.Hive环境搭建 1. 准备安装文件 下载地址: http://archive.cloudera.com/cd ...

  5. [ionic开源项目教程] - 第5讲 如何在项目中使用全局配置

    第5讲 如何在项目中使用全局配置? Q:ionic开发,说纯粹一点,用的就是html+css+js,那么无疑跟web开发的方式是类似的.在这里给大家分享一个小技巧,如何在项目中使用全局配置? A:我的 ...

  6. 设置app的状态栏样式

    http://www.jianshu.com/p/9f7f3fa624e7 http://cocoa.venj.me/blog/view-controller-based-status-bar-sty ...

  7. dom4j创建格式化的xml文件

    import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java. ...

  8. Java Socket 模拟HTTP请求

    public static void main(String[] args) { try { String url = "192.168.1.103"; Socket socket ...

  9. hdu 1226 超级密码(bfs+余数判重)

    题意:略过 分析:用m个数字组成一个能够被n整除的c进制数的最小值,实际上本题的关键就在于这个最小值上.  首先确定我们的思路是从小到大寻找.先查看一位数,即查看着m个数字是否能被n整除:若不能,就查 ...

  10. Android Terminal telnet windows

    /******************************************************************************************** * Andr ...