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 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:
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.
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
]
每次输入两个字符串,一个是源串,一个是待匹配的串。对于源字符串中的字符,可以进行入栈操作。入栈的话没有条件,当栈内元素个数小于源字符串中字符个数时,都可以入栈。但是出栈就不同了,出栈的时候首先需要将栈顶的元素和待匹配串中的字符进行比对(匹配的规则是:从左往右匹配依次 待匹配串中所有的字符)。当匹配成功时,可以进行出栈操作,但前提是栈非空(即npop<npush,也就是入栈的字符个数大于出栈的字符个数)
#include<cstdio>
#include<iostream>
#include<stack>
using namespace std;
string source,target;
char s[20];
int top=0;
char operation[20];
int len;
//depth记录递归的深度,npush记录入栈的次数,npop记录出栈的次数
//可以看成二叉树,递归的时候只有两个分支,入栈和出栈,根据不同的条件选择不同的操作
void dfs(int npush,int npop) {
if(npush==len&&npop==len) {
for(int i=0; i<2*len; i++) {
printf("%c ",operation[i]);
}
printf("\n");
return;
}
//因为每一个进出栈是有顺序的,所以可以首先让进栈操作先执行,然后让出栈操作执行(i<o)
if(npush < len) { //如果进栈的次数小于源字符串的长度,则进栈
s[top++]=source[npush];
operation[npush+npop]='i'; //operation记录的是过程,不需要回溯。进栈的次数+出栈的次数=操作的总次数
dfs(npush+1,npop);//入栈
top--;
}
//因为两个分支都要执行,所以这里使用if语句,不适用 else if语句。
//出栈的次数小于进栈的次数(说明栈s内存在元素),并且栈顶元素和target的npop下标处元素相同
if(npop<npush && s[top-1]==target[npop]) { //则,可出栈
top--;
operation[npush+npop]='o';
dfs(npush,npop+1);
s[top++]=target[npop];//回溯,反出栈==入栈
}
}
int main() {
//freopen("in.txt","r",stdin);
while(cin>>source>>target) {
len=source.length();
printf("[\n");
dfs(0,0);
printf("]\n");
}
return 0;
}