Fork me on GitHub

HDU ACM 1515 Anagrams by Stack

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后面都跟有空格,而且每次当前出来的字符的字符串都要跟目的字符串进行比较以减少不必要的情况,减小耗时

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<cmath>
 4 #include<stack>
 5 #include<algorithm>
 6 #include<stdlib.h>
 7 #define SIZE 1000
 8 
 9 using namespace std;
10 char res[SIZE];
11 char input[SIZE];
12 char elem[SIZE];
13 int opera[SIZE];
14 int len;
15 stack<char>sample;
16 
17 void Traverse(int elen, int rlen, int oplen)
18 {
19     if(rlen == len)
20     {
21         bool flag = true;
22         for(int i=0; i<len; ++i)
23             if(res[i] != input[i])
24             {
25                 flag = false;
26                 break;
27             }
28         if(flag)
29         {
30             for(int i=0; i<oplen; ++i)
31                 printf("%c ", opera[i]);
32             printf("\n");
33         }
34         else return;
35     }
36     bool flag = true;
37     for(int i=0; i<rlen; ++i)
38     if(res[i] != input[i])
39     {
40         flag = false;
41         break;
42     }
43     if(!flag) return;
44     
45     
46     if(elen<len)
47     {
48         sample.push(elem[elen]);
49         opera[oplen] = 'i';
50         Traverse(elen+1, rlen, oplen+1);
51         sample.pop();
52     }
53     if(!sample.empty())
54     {
55         res[rlen] = sample.top();
56         sample.pop();
57         opera[oplen] = 'o';
58         Traverse(elen, rlen+1, oplen+1);
59         sample.push(res[rlen]);
60     }
61     return;
62 }
63 
64 int main()
65 {
66     #ifndef ONLINE_JUDGE
67     freopen("input.txt", "r", stdin);
68     #endif
69     while(scanf("%s", elem) != EOF)
70     {
71         scanf("%s", input);
72         printf("[\n");
73         len = strlen(input);
74         if(len == strlen(elem))
75             Traverse(0, 0, 0);
76         printf("]\n");
77     }
78     return 0;
79 }

 

posted @ 2013-07-17 19:03  Gifur  阅读(673)  评论(0编辑  收藏  举报
TOP