最省納智捷1年新車83萬起5檔高息股比定存強10倍吉隆坡最便宜行程一覽表不捨學生找「槌」 高中...
2012-04-27 07:01:39 人氣(216) | 回應(0) | 推薦(0) | 收藏(0) 上一篇 | 下一篇

[UVA][組合] 124 - Following Orders

0
收藏
0
推薦


 Following Orders 

Background

Order is an important concept in mathematics and in computer science. For example, Zorn's Lemma states: ``a partially ordered set in which every chain has an upper bound contains a maximal element.'' Order is also important in reasoning about the fix-point semantics of programs.

This problem involves neither Zorn's Lemma nor fix-point semantics, but does involve order.

The Problem

Given a list of variable constraints of the form x < y, you are to write a program that prints all orderings of the variables that are consistent with the constraints.

For example, given the constraints x < y and x < z there are two orderings of the variables x, y, and z that are consistent with these constraints: x y z and x z y.

The Input

The input consists of a sequence of constraint specifications. A specification consists of two lines: a list of variables on one line followed by a list of constraints on the next line. A constraint is given by a pair of variables, where x y indicates that x < y.

All variables are single character, lower-case letters. There will be at least two variables, and no more than 20 variables in a specification. There will be at least one constraint, and no more than 50 constraints in a specification. There will be at least one, and no more than 300 orderings consistent with the contraints in a specification.

Input is terminated by end-of-file.

The Output

For each constraint specification, all orderings consistent with the constraints should be printed. Orderings are printed in lexicographical (alphabetical) order, one per line.

Output for different constraint specifications is separated by a blank line.

Sample Input

a b f g
a b b f
v w x y z
v y x v z v w v

Sample Output

abfg
abgf
agbf
gabf

wxzvy
wzxvy
xwzvy
xzwvy
zwxvy
zxwvy


#include <stdio.h>
#include <string.h>
#include <iostream>
#include <sstream>
using namespace std;

int can[26], map[26][26], used[26];
char str[26];
void DFS(int idx, int n) {
    if(idx == n) {
        str[idx] = '\0';
        puts(str);
        return;
    }
    int i, j;
    for(i = 0; i < 26; i++) {
        if(can[i] == 1 && used[i] == 0) {
            for(j = 0; j < 26; j++)
                if(map[i][j] == 1 && used[j] == 1)
                    return;
            used[i] = 1;
            str[idx] = i+'a';
            DFS(idx+1, n);
            used[i] = 0;
        }
    }
}
int main() {
    stringstream sin;
    string line;
    char ch1, ch2, first = 0;
    while(getline(cin, line)) {
        if(first)
            puts("");
        first = 1;
        memset(can, 0, sizeof(can));
        memset(map, 0, sizeof(map));
        memset(used, 0, sizeof(used));
        sin.clear();
        sin << line;
        while(sin >> ch1) {
            can[ch1-'a'] = 1;
        }
        int n = 0;
        for(int i = 0; i < 26; i++)
            n += can[i];
        getline(cin, line);
        sin.clear();
        sin << line;
        while(sin >> ch1 >> ch2) {
            map[ch1-'a'][ch2-'a'] = 1;
        }
        DFS(0, n);
    }
    return 0;
}


124Following Orders組合
台長:Morris
人氣(216) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][二分圖檢查] 11396 - Claw Decomposition
此分類上一篇:[UVA][priority_queue] 1203 - Argus

我要回應
是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入以下數字 (ex:123)

(有*為必填)
詳全文