Anagrams by Stack
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1513 Accepted Submission(s): 690
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
]
非常有意思的题目,栈模拟,通过栈模拟,将ts1实现ts2,题目要求字典序,i<o,所以只要尽量进栈优先就可以了
本题还要注意一个问题,栈最后可能不为空,考虑ts1="abcde" ts2="bcde" 最后‘a‘还留在栈中,所以最好要清空栈,写的时候没想到,现在写题解突然想到原因了...
这题值得以后重复研究,挺经典的样子,好久没做这种存dfs的题目了,能想到一半,但是自己不看题解能难理清思绪,题解在代码里
/*programming ts1全部入栈,栈顶元素和ts2的指针指向的元素比较,如果不匹配,栈顶元素弹出,即ts1的指针前移,继续栈顶匹配, 直到ts1的某个元素和ts2匹配,栈顶元素出栈,ts2的指针后移,原来出栈的元素继续入栈,重复此过程,如果ts2的元素被匹配完全,说明可行,输出记录的过程way 因为最后一步操作是出栈,所以输出的时候,出栈完之后要重新入栈 */ #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <algorithm> #include <stack> #include <queue> #include <string> const int inf = (1<<31)-1; const int MAXN = 1e4; using namespace std; stack<char>ms; char ts1[MAXN]; char ts2[MAXN]; int len1,len2; char way[2*MAXN]; void dfs(int i,int j,int k){ if(j==len2){ for(int l=0;l<k;l++){ cout<<way[l]<<" "; } cout<<endl; return ; } if(i!=len1){ ms.push(ts1[i]); way[k] = ‘i‘; dfs(i+1,j,k+1); ms.pop();//回溯出栈,总出口 } if(!ms.empty()){ if(ms.top()==ts2[j]){ way[k] =‘o‘; ms.pop(); dfs(i,j+1,k+1); ms.push(ts2[j]);//输出完之后回溯进栈 } } } int main() { while(scanf("%s%s",ts1,ts2)!=EOF){ len1 = strlen(ts1); len2 = strlen(ts2); cout<<"["<<endl; dfs(0,0,0); cout<<"]"<<endl; } //cout << "Hello world!" << endl; return 0; }