Anagrams by Stack(深度优先搜索)
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:
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后面都有空格
1 #include <iostream> 2 #include <stack> 3 #include <cstdio> 4 #include <cstring> 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<char> 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 else 24 { 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 else 63 { 64 while(!sta.empty()) sta.pop(); 65 printf("[\n"); 66 DFS(0, 0, 0); 67 printf("]\n"); 68 } 69 } 70 return 0; 71 }