HDU 1515 - Anagrams by Stack

http://acm.hdu.edu.cn/showproblem.php?pid=1515

Anagrams by Stack

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 552    Accepted Submission(s): 284


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
]
 

刚到这道题,我以为是bfs模拟,后来看到字典序输出,果断认为是dfs加回溯~~~通过对堆栈的模拟来dfs,实现字符串的转换~~~

这道题pe了一次,很郁闷。。。。原来在zoj上做的时候,最后多输出一个空格,算pe,结果现在少输出个,pe了,加上就好了~~无语ing

ps:这两天做搜索一直不顺,上次碰到了个优先队列广搜加输出路径的(1026),刚开始一直出错,后来测试用例过了,一直wa,改的我要疯,最后放弃了~~~又碰到一个经典的八连块问题,我想不起来怎么存节点(vis),结果在白书上看的用查找表,结果测试用例过了但是超时,看讨论区说用双广搜,不会,纠结~~~本来想把错代码贴上,再在网上找个对的,后来想想还没理解,就不要误导人了,没贴。。。以后继续努力吧~~)

代码如下:

 1 #include<iostream>
2 #include<stack>
3 #include<cstring>
4 #include<cstdio>
5 #include<stdlib.h>
6 using namespace std;
7 char start[100], end[100], b[200], s1[100], s2[100];
8 int len1, len2;
9 stack<char> s;
10 int cmp(const void *a, const void *b)
11 {
12 return *(char *)a - *(char *)b;
13 }
14 void dfs(int cur1, int cur2, int step)
15 {
16 int i;
17 if(cur2 == len2)
18 {
19 for(i = 0; i < step; i++)
20 {
21 printf("%c ", b[i]);
22 }
23 putchar(10);
24 return;
25 }
26 if(cur1 < len1)
27 {
28 s.push(start[cur1]);
29 b[step] = 'i';
30 dfs(cur1+1, cur2, step+1);
31 s.pop();
32 }
33 if(cur2 < len2 && !s.empty())
34 {
35 if(end[cur2] == s.top())
36 {
37 s.pop();
38 b[step] = 'o';
39 dfs(cur1, cur2+1, step+1);
40 s.push(end[cur2]);
41 }
42 }
43 }
44 int main()
45 {
46 while(scanf("%s%s", start, end) != EOF)
47 {
48 strcpy(s1, start);
49 strcpy(s2, end);
50 len1 = strlen(start);
51 len2 = strlen(end);
52 qsort(s1, len1, sizeof(char), cmp);
53 qsort(s2, len2, sizeof(char), cmp);
54 if(len1 == len2 && !strcmp(s1, s2))
55 {
56 printf("[\n");
57 dfs(0, 0, 0);
58 printf("]\n");
59 }
60 else printf("[\n]\n");
61 }
62 return 0;
63 }

 

posted @ 2011-12-24 20:09  枫萧萧  阅读(1406)  评论(6编辑  收藏  举报