程序控

IPPP (Institute of Penniless Peasent-Programmer) Fellow

:: :: :: :: :: :: :: ::
 77 随笔 :: 0 文章 :: 442 评论 :: 0 引用

Time limit: 3.000 seconds

Background背景

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.

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 i o o是有效的，但
• i i o is not (it's too short),
i i o是无效的，（没有完成操作）
• neither is i i o o o i (there's an illegal pop of an empty stack)
i i o o o i同样无效（执行了非法的从空栈弹出的操作）

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.

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
]

Solution解答

#include <iostream>
#include <string>
#include <vector>
using namespace std;
//深度遍例所有可能的进出栈序列，进栈视为左子树，出栈视为右子树
void GenAnagram(vector<char> &Src, vector<char> &Dest,
vector<char> &Order, vector<char> &Stack) {
//如果字符串不为空，则执行进栈操作，相当于遍例左子树
if (!Src.empty()) {
//在操作序列中加入i
Order.push_back('i');
//进栈并保存现场
Stack.push_back(Src.back());
Src.pop_back();
//以当前状态遍例下一个动作
GenAnagram(Src, Dest, Order, Stack);
//恢复现场
Src.push_back(Stack.back());
Stack.pop_back();
Order.pop_back();
}
//如果栈不为空则执行弹栈操作，当栈顶和目标串相应字符相等时才继续
if (!Stack.empty() && Stack.back() == Dest[Stack.size() + Src.size() - 1]) {
//在操作序列中加入o
Order.push_back('o');
//弹栈并保存现场
char cTemp = Stack.back();
Stack.pop_back();
//以当前状态遍例下一个动作
GenAnagram(Src, Dest, Order, Stack);
//恢复现场
Stack.push_back(cTemp);
Order.pop_back();
}
//如果原字符串已空，且栈也为同，则说明所有字符都已出栈
if (Src.empty() && Stack.empty()) {
//输出进出栈操作序列
vector<char>::iterator i = Order.begin();
for (; i != Order.end() - 1; ++i ) {
cout << *i << ' ';
}
cout << *i << endl;
}
}
//主函数
int main(void) {
//循环处理输入的每一对字符串
for (string str1, str2; cin >> str1 >> str2; cout << ']' << endl) {
cout << '[' << endl;
//当两字符串长度相等且不为空时才运算
if (str1.length() == str2.length() && !str1.empty()) {
//建立原串、目标串、字符栈和结果数组
vector<char> Src(str1.rbegin(), str1.rend());
vector<char> Dest(str2.rbegin(), str2.rend());
vector<char> Order, Stack;
//执行深度遍例，输出所有结果
GenAnagram(Src, Dest, Order, Stack);
}
}
return 0;
}
posted on 2010-08-04 16:02  Devymex  阅读(...)  评论(...编辑  收藏