首页 > ACM题库 > UVA > UVa OJ 732 – Anagrams by Stack (堆栈字迷)
2013
12-30

UVa OJ 732 – Anagrams by Stack (堆栈字迷)

Time limit: 3.000 seconds
限时:3.000秒

 

Background
背景

How can anagrams result from sequences of stack operations? There are two sequences of stack operators which can convert “TROT” to “TORT”:
怎样才能通过一系列的堆栈操作,将字符串按要求的顺序重新排列?比如有两种进出栈序列可以将字符串“TROT”转换为“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.
其中i表示压入,o表示弹出。给定一对单词,你要写一个程序通过一组进出栈的操作,使前一个单词的字母顺序变成后一个单词。

 

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.
输入由多行组成,每对单词占2行。第1行为原始单词(不包括行尾的换行符),第2行为目标单词(同样不包括行尾的换行符)。

 

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.
序列中每组操作应该按字典顺序输出。在一组操作中,i和o用空格隔开,每一组独占一行。

 

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”来表示进栈,“o”表示出栈,栈的初始状态为空。给定一个输入的单词,只要对每一个字母都进行了进栈和出栈操作,这一组操作才可能是有效的。进一步来说,不能对空栈执行弹出操作。比如对于输入的单词FOO,下面的操作序列:

  • 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.
可能有多组有效操作都能够为输入的单词生成指定的字母排列。比如对于输入的单词“FOO”,操作i i o i o o就可以生成重排的单词“OOF”。操作i i i o o o也可以生成同样的重排。你要写一个程序,找出所有有效的操作序列,以使输入的一对单词的前者重排为后者。

 

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
]

 

Analysis
分析

这道题必须遍例所有可能的进出栈序列才能求得全部解。如果将进出栈序列视为二叉树,那么求解的过程就是一个典型的深度遍例。进栈为左子节点,出栈为右子节点。当所有字符都已出栈,即遍例到了叶子节点,应打印输出前面的进出栈序列(也可以视为从根到叶子的路径)。如果在弹栈时发现当前弹出的字符与目标串中的该字符位置不符,则无需再遍例这一支的子节点,直接回溯即可。思路很简单,就看实现了。注意,不要在行尾输出空格。

 

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;
}

转自:http://www.cnblogs.com/devymex/archive/2010/08/04/1792276.html


  1. 题目需要求解的是最小值,而且没有考虑可能存在环,比如
    0 0 0 0 0
    1 1 1 1 0
    1 0 0 0 0
    1 0 1 0 1
    1 0 0 0 0
    会陷入死循环

  2. #!/usr/bin/env python
    def cou(n):
    arr =
    i = 1
    while(i<n):
    arr.append(arr[i-1]+selfcount(i))
    i+=1
    return arr[n-1]

    def selfcount(n):
    count = 0
    while(n):
    if n%10 == 1:
    count += 1
    n /= 10
    return count

  3. 题本身没错,但是HDOJ放题目的时候,前面有个题目解释了什么是XXX定律。
    这里直接放了这个题目,肯定没几个人明白是干啥

  4. Gucci New Fall Arrivals

    This is really nice to know. I hope it will be successful in the future. Good job on this and keep up the good work.

  5. #include <cstdio>
    #include <algorithm>

    struct LWPair{
    int l,w;
    };

    int main() {
    //freopen("input.txt","r",stdin);
    const int MAXSIZE=5000, MAXVAL=10000;
    LWPair sticks[MAXSIZE];
    int store[MAXSIZE];
    int ncase, nstick, length,width, tmp, time, i,j;
    if(scanf("%d",&ncase)!=1) return -1;
    while(ncase– && scanf("%d",&nstick)==1) {
    for(i=0;i<nstick;++i) scanf("%d%d",&sticks .l,&sticks .w);
    std::sort(sticks,sticks+nstick,[](const LWPair &lhs, const LWPair &rhs) { return lhs.l>rhs.l || lhs.l==rhs.l && lhs.w>rhs.w; });
    for(time=-1,i=0;i<nstick;++i) {
    tmp=sticks .w;
    for(j=time;j>=0 && store >=tmp;–j) ; // search from right to left
    if(j==time) { store[++time]=tmp; }
    else { store[j+1]=tmp; }
    }
    printf("%dn",time+1);
    }
    return 0;
    }