【【搜索题】】
【题目】
Anagrams by Stack
Time Limit: 1 Second Memory Limit:32768 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 ofi ando 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, eachi ando 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 sequencei i o i o o produce the anagram OOF. So also would the sequencei i i o o o. You are to write a program to input pairs of words and output all the valid sequences ofi ando 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
]
Source: Zhejiang University Local Contest 2001
【题意说明】
给定一个原始字符串和目标字符串,利用将原始字符串按串字母的顺序进行入栈、出栈的组合操作得到目标字符串,问有几种方式并输出操作方法。所谓将串字母进行入栈、出栈的组合操作得到新字符串,举例说明:比如字符串abc从a到c依次进行入栈、出栈操作有(1)a(入栈)、a(出栈)、b(入栈)、b(出栈)、c(入栈)、c(出栈) 则依出栈顺序得串abc;(2)a(入栈)、b(入栈)、b(出栈)、a(出栈)、c(入栈)、c(出栈)则依出栈顺序得串bac;等等,用i、o表示串字母的入、出栈操作则上述abc串的2个操作可表述为(1)ioioio(2)iiooio。所以题意即要求用io组合输出从原始串到目标串有哪几种入出栈操作组合方式。
【解答】
(一)分析:用深度优先搜索可解。深搜树上每个节点表示push(i操作)或pop(o操作),并随时记录pop之后得到的当前串值。当某条深搜路径长度满足其上的pop数目为串长时,比较得到的当前串值是否与目标串相同,若相同则输出该路径。
(二)代码:
#include<iostream>
#include<stack>
#include<cstring>
using namespace std;
char s1[50],s2[50],stemp[50],op[100];//源字符串,目标字符串,临时字符串,临时操作数组
stack<char> st;
int len,num,inum,onum;//字符串长,操作数组下标,push操作数目,pop操作数目
void dfs(int i)//递归深搜函数
{
if(onum==len)/*一定要注意,在DFS树的一条路径上一定要等pop操作数等于串长时候才能进行判断*/
{
stemp[onum]='\0';
if(strcmp(s2,stemp)==0)//找到一种解决方案
{
int j;
for(j=0;j<num;j++)
cout<<op[j]<<' ';
cout<<endl;
}
return;
}
if(inum<len)//入栈模拟
{
st.push(s1[i+1]);
inum++;
op[num++]='i';
dfs(i+1);
st.pop();
inum--;
num--;
}
if(onum<inum)//出栈模拟
{
char ch=st.top();
stemp[onum++]=ch;
st.pop();
op[num++]='o';
dfs(i);
st.push(ch);//notice
onum--;
num--;
}
}
int main()
{
while(cin>>s1){
cin>>s2;
while(!st.empty())
st.pop();
num=0;
inum=0;
onum=0;
len=strlen(s1);
op[num++]='i';
st.push(s1[0]);
inum++;
cout<<'['<<endl;
dfs(0);
cout<<']'<<endl;
}
return 0;
}
//Accepted
(解于2009/10)