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. Android Socket 聊天室示例

    服务端: package com.test.chatServer; import java.io.IOException; import java.net.ServerSocket; import j ...

  2. Android开源库--SlidingMenu左右侧滑菜单

    如果说我比别人看得更远些,那是因为我站在了巨人的肩上.   github地址:https://github.com/jfeinstein10/SlidingMenu   设置: 1.下载之后以依赖项的 ...

  3. datagridview的某些属性以及增删改查

    private void button1_Click(object sender, EventArgs e) //查询 { dataGridView1.AutoGenerateColumns = fa ...

  4. Android利用Http下载文件

    Android利用Http下载文件 一.场景 下载存文本文件和下载如mp3等大容量的文件 界面 二.代码编写 1.AndroidMainfest.xml中配置 主要是解决网络权限和写SDCard的权限 ...

  5. Balsamiq Mockups简单介绍(UI草图绘制工具)

    什么是Balsamiq Mockups Balsamiq Mockups出自加利福尼亚州的Balsamiq工作室,创始人Peldi在2008年6月推出了这款手绘风格的产品原型设计工具,并广受好评.2年 ...

  6. UVa 11210 (DFS) Chinese Mahjong

    大白书第一章的例题,当时看起来很吃力,现如今A这道题的话怎么写都无所谓了. 思路很简单,就是枚举胡哪张牌,然后枚举一下将牌,剩下如果能找到4个顺子或者刻子就胡了. 由于粗心,34个字符串初始化写错,各 ...

  7. HDU 1158 Employment Planning【DP】

    题意:给出n个月,雇佣一个人所需的钱hire,一个人工作一个月所需要的钱salary,解雇一个人所需要的钱fire,再给出这n个月每月1至少有num[i]个人完成工作,问完成整个工作所花费的最少的钱是 ...

  8. 【转】很有用但鲜有人知的 Linux 命令

    Linux命令行吸引了大多数Linux爱好者.一个正常的Linux用户一般掌握大约50-60个命令来处理每日的任务.Linux命令和它们的转换对于Linux用户.Shell脚本程序员和管理员来说是最有 ...

  9. 部署在IIS服务器的asp.net 网站,禁止访问指定类型文件

    网站上的一些文件不希望用户访问,可以通过下面的方式简单实现.不需写代码(在IIS6下试验过). 第一步,在IIS中实现映射. 哪些文件需要特殊处理. 通俗的将就是将哪种类型的文件交给特定的工厂来处理. ...

  10. 手机GUI自动化测试介绍

    手机GUI自动化测试介绍 Posted on 2013/05/15 Xing Binbin(测试工程师) 摘要 众所周知,自动化测试可以一定程度上减轻测试人员负担,提高测试效率,并且通过自动化还可以实 ...