zoukankan      html  css  js  c++  java
  • POJ1270 Following Orders (拓扑排序)

    Following Orders
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 4254   Accepted: 1709

    Description

    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
    
    收获:1.了解了stringstream.
       2.用dfs输出拓扑排序的所有情况。
    #include <cstdio>
    #include <iostream>
    #include <cstdlib>
    #include <algorithm>
    #include <ctime>
    #include <cmath>
    #include <string>
    #include <cstring>
    #include <stack>
    #include <queue>
    #include <list>
    #include <vector>
    #include <map>
    #include <set>
    #include <sstream>
    using namespace std;
    
    const int INF=0x3f3f3f3f;
    const double eps=1e-10;
    const double PI=acos(-1.0);
    #define maxn 500
    #define maxm 28
    char a[maxn];
    char ans[maxn];
    int in[maxn];
    int vis[maxn];
    int map1[maxn][maxn];
    int total;
    void dfs(int id)
    {
        if(id == total)
        {
            ans[id] = '';
            puts(ans);
            return;
        }
        for(int i = 0; i < 26; i++)
        {
            if(vis[i]) continue;
            if(in[i] == 0)
            {
                ans[id] = 'a' + i;
                vis[i] = 1;
                for(int j = 0; j < 26; j++)
                    if(map1[i][j]) in[j]--;
                dfs(id+1);
                vis[i] = 0;
                for(int j = 0; j < 26; j++)
                    if(map1[i][j]) in[j]++;
            }
        }
    }
    char x, y;
    int main()
    {
        int flag = 0;
        while(gets(a) != NULL)
        {
            if(flag)
                puts("");
            flag = 1;
            total = 0;
            stringstream ss(a);
            memset(in, INF, sizeof in);
            memset(vis, 0, sizeof vis);
            while(ss >> x)
            {
            in[x - 'a'] = 0;
            total++;
            }
            gets(a);
            stringstream sss(a);//读取一行。
            memset(map1, 0, sizeof map1);
            while(sss >> x >> y)//扫描该行的字符。
            {
                map1[x - 'a'][y- 'a'] = 1;
                in[y - 'a']++;
    
            }
            dfs(0);
        }
        return 0;
    }
  • 相关阅读:
    最近有人说我欺骗消费者,今天来一波视频分享
    前端 Java Python等资源合集大放送
    dubbo源码学习(四):暴露服务的过程
    dubbo源码学习(二) : spring 自定义标签
    Dubbo多注册中心和Zookeeper服务的迁移
    线程各种状态转换分析
    java并发之同步辅助类CountDownLatch
    工作5年的Java程序员,才学会阅读源码,可悲吗?
    【阿里面试系列】Java线程的应用及挑战
    「阿里面试系列」搞懂并发编程,轻松应对80%的面试场景
  • 原文地址:https://www.cnblogs.com/ZP-Better/p/4736824.html
Copyright © 2011-2022 走看看