博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Anagrams by Stack(深度优先搜索)
阅读量:6329 次
发布时间:2019-06-22

本文共 3991 字,大约阅读时间需要 13 分钟。

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 oi 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:

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

madamadammbahamabahamalongshortericrice

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:
1 #include 
2 #include
3 #include
4 #include
5 6 using namespace std; 7 8 const int MAX_LEN = 50; 9 char s[MAX_LEN], d[MAX_LEN], ans[MAX_LEN];10 stack
sta;11 12 void DFS(int si, int di, int ansi) //三个参数分别是s,d,ans的下标13 {14 if(s[si] == '\0')15 {16 if(di == si)17 {18 for(int i = 0; i < ansi; i++)19 printf("%c ", ans[i]);20 puts("");21 return ;22 }23 else24 {25 if(!sta.empty() && sta.top() == d[di])26 {27 char t = sta.top();28 sta.pop();29 ans[ansi] = 'o';30 DFS(si, di + 1, ansi + 1);31 sta.push(t);32 return ;33 }34 else return ;35 }36 }37 sta.push(s[si]);38 ans[ansi] = 'i';39 DFS(si + 1, di, ansi + 1);40 if(!sta.empty()) sta.pop();41 if(!sta.empty() && sta.top() == d[di])42 {43 char t = sta.top();44 sta.pop();45 ans[ansi] = 'o';46 DFS(si, di + 1, ansi + 1);47 sta.push(t);48 return ;49 }50 }51 52 int main()53 {54 while(scanf("%s %s", s, d) != EOF)55 {56 int lens = strlen(s);57 int lend = strlen(d);58 if(lens != lend)59 {60 printf("[\n]\n");61 }62 else63 {64 while(!sta.empty()) sta.pop();65 printf("[\n");66 DFS(0, 0, 0);67 printf("]\n");68 }69 }70 return 0;71 }

 


转载地址:http://lwfoa.baihongyu.com/

你可能感兴趣的文章
Windows 下 tail 查看日志命令工具分享
查看>>
模版编程--类模版
查看>>
win32下创建窗口
查看>>
10个随机数相加等于100
查看>>
105、Replicated Mode VS Global Mode (Swarm12)
查看>>
13-计算最长英语单词链
查看>>
asp快速开发方法之分页函数
查看>>
关于网易云音乐爬虫的api接口?
查看>>
轻量级还是重量级
查看>>
关于Android LogCat不打印日志输出的问题
查看>>
【洛谷 P2464】[SDOI2008]郁闷的小J(线段树)
查看>>
iOS学习07之C语言指针
查看>>
OS开发UI基础—手写控件,frame,center和bounds属性
查看>>
简单的邮件发送
查看>>
mysql性能优化分析 --- 上篇
查看>>
<TCP/IP>ICMP报文的分类
查看>>
Jvm垃圾回收器(终结篇)
查看>>
ajax发起和收到服务器的信息
查看>>
SPOJ TTM
查看>>
HDU-2159 FATE (DP)
查看>>