zoukankan      html  css  js  c++  java
  • Day4

    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.
    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.

    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 contraints 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.

    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

    思路:
    简单的拓扑排序+dfs,通过入度判断,从1到26就有字典序,代码如下:
    int in[30], G[30][30], vis[30], print[30], num;
    char ans[25];
    
    void init() {
        num = 0;
        memset(in, 0, sizeof(in));
        memset(G, 0, sizeof(G));
        memset(vis, 0, sizeof(vis));
        memset(print, 0, sizeof(print));
    }
    
    void dfs(int u, int cnt) {
        ans[cnt] = u - 1 + 'a';
        if(cnt == num) {
            for(int i = 1; i <= cnt; ++i)
                cout << ans[i];
            cout << "
    ";
            return;
        }
        // mark the point
        print[u] = 1;
        for(int i = 1; i <= 26; ++i) {
            if(vis[i] && G[u][i]) in[i]--;
        }
        for(int i = 1; i <= 26; ++i) {
            if(vis[i] && !print[i] && !in[i]) {
                dfs(i, cnt+1);
            }
        // backtracing
        }
        for(int i = 1; i <= 26; ++i) {
            if(vis[i] && G[u][i]) in[i]++;
        }
        print[u] = 0;
    }
    
    int main() {
        ios::sync_with_stdio(false);
        string t;
        int t1, t2;
        while(getline(cin, t)) {
            init();
            int siz = t.size();
            for(int i = 0; i < siz; i += 2) {
                vis[t[i] - 'a' + 1] = 1;
                num++;
            }
            getline(cin, t);
            siz = t.size();
            for(int i = 0; i + 2 < siz; i += 4) {
                t1 = t[i] - 'a' + 1, t2 = t[i+2] - 'a' + 1;
                G[t1][t2] = 1, in[t2]++;
            }
            for(int i = 1; i <= 26; ++i) {
                if(vis[i] && !in[i]) {
                    dfs(i, 1);
                }
            }
            cout << "
    ";
        }
        
        return 0;
    }
    View Code
  • 相关阅读:
    2019.9.18 Unity3D与Android相互传递消息 & unity与ios相互传递消息
    2019.9.10 IEnumerable 详解C# 迭代器
    Windows Live Writer 之 代码快速插入插件
    目标管理剖析与实践– 献给追梦的人 (转)
    转:简历写法
    Linux下 输入 env 而得到的环境变量解读
    how to install tweepy
    全分布式环境下,DataNode不启动的问题解决
    几个因为hadoop配置文件不当造成的错误
    Hadoop: HDFS 格式化时,出现 “ERROR namenode.NameNode: java.io.IOException: Cannot create directory /usr/hadoop/tmp/dfs/name/current”
  • 原文地址:https://www.cnblogs.com/GRedComeT/p/12170849.html
Copyright © 2011-2022 走看看