ZOJ Problem Set - 1004
Anagrams by Stack

Time Limit: 2 Seconds      Memory Limit: 65536 KB

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.

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.

Process

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

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
]

深度优先搜索即可。注意输出格式,每个i或者o后面都有空格

AC Code:
 #include <iostream>
#include <stack>
#include <cstdio>
#include <cstring> using namespace std; const int MAX_LEN = ;
char s[MAX_LEN], d[MAX_LEN], ans[MAX_LEN];
stack<char> sta; void DFS(int si, int di, int ansi) //三个参数分别是s,d,ans的下标
{
if(s[si] == '\0')
{
if(di == si)
{
for(int i = ; i < ansi; i++)
printf("%c ", ans[i]);
puts("");
return ;
}
else
{
if(!sta.empty() && sta.top() == d[di])
{
char t = sta.top();
sta.pop();
ans[ansi] = 'o';
DFS(si, di + , ansi + );
sta.push(t);
return ;
}
else return ;
}
}
sta.push(s[si]);
ans[ansi] = 'i';
DFS(si + , di, ansi + );
if(!sta.empty()) sta.pop();
if(!sta.empty() && sta.top() == d[di])
{
char t = sta.top();
sta.pop();
ans[ansi] = 'o';
DFS(si, di + , ansi + );
sta.push(t);
return ;
}
} int main()
{
while(scanf("%s %s", s, d) != EOF)
{
int lens = strlen(s);
int lend = strlen(d);
if(lens != lend)
{
printf("[\n]\n");
}
else
{
while(!sta.empty()) sta.pop();
printf("[\n");
DFS(, , );
printf("]\n");
}
}
return ;
}

最新文章

  1. Java多线程之构造与发布
  2. 用原生DOM 遍历页面节点
  3. Form居中显示
  4. java综合
  5. 蓝凌表单的表体调用Javascript
  6. HttpHandler与HttpModule及实现文件下载
  7. z-index解决弹出层遮罩层覆盖子div不能显示输出的问题
  8. (转)在 Visual Studio 2010 中创建 ASP.Net Web Service
  9. datalog
  10. cURL模拟网页登陆
  11. nyoj234 吃土豆 01背包
  12. Gradle 1.12用户指南翻译——第五十章. 依赖管理
  13. Android为TV端助力:EventBus跨进程发送消息
  14. 信息技术手册可视化进度报告 基于BeautifulSoup框架的python3爬取数据并连接保存到MySQL数据库
  15. $Django 表设计,登陆图片验证
  16. 从0移植uboot(三) _编译最小可用uboot
  17. 使用react封装评论组件
  18. 第 3 章 镜像 - 021 - Docker 镜像小结
  19. nested exception is java.lang.IllegalStateException: No persistence units parsed from {classpath*:META-INF/persistence.xml}
  20. Log4net (Log for .net)

热门文章

  1. SSH新学,关于面向对象的看法
  2. 敏捷开发 Scrum 综述
  3. “来用”Beta版使用说明
  4. python 中如何计算时间差...
  5. Numpy and Pandas
  6. C/C++学习计划
  7. ssh &amp; sftp &amp; MacOS
  8. android面试(3)---基本问题
  9. [COGS1000]伊吹萃香 最短路
  10. 命令行下django-admin.py参数不起作用的问题解决