1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > 【zoj1004 vector stack STL】anagrams by stack

【zoj1004 vector stack STL】anagrams by stack

时间:2024-02-17 01:10:29

相关推荐

【zoj1004  vector stack STL】anagrams by stack

点击打开链接

Anagrams by StackTime 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 oi o i i o o i o]

whereistands for Push andostands 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 ofiandowhich 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, eachiandois 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 symboli(in) for push ando(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:

Valid sequences yield rearrangements of the letters in an input word. For example, the input word FOO and the sequencei i o i o oproduce 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 ofiandowhich will produce the second member of each pair from the first.

Sample Input

madamadammbahamabahamalongshortericrice

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

SubmitStatus

用回溯算法将每一种可能性都搜索出来(采用完全二叉树的搜索结构)

AC代码:

#include<iostream>#include<vector>#include<stack>using namespace std;string a,b;vector<char>operate;stack<char>build;int length;void dfs(int iPush,int iPop){if(iPush==length&&iPop==length){for(int i=0;i<operate.size();i++)cout<<operate[i]<<" ";cout<<endl;}if(iPush+1<=length){build.push(a[iPush]);operate.push_back('i');dfs(iPush+1,iPop);build.pop();operate.pop_back();}if(iPop+1<=iPush&&iPop+1<=length&&build.top()==b[iPop]){char tc=build.top();build.pop();operate.push_back('o');dfs(iPush,iPop+1);build.push(tc);operate.pop_back();} } int main(){while(cin>>a>>b){length=a.length();cout<<"["<<endl;dfs(0,0);cout<<"]"<<endl;}return 0;}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。