zoukankan      html  css  js  c++  java
  • [POJ] 1270 Following Orders

    Following Orders
    Time Limit: 1000MS      Memory Limit: 10000K
    Total Submissions: 5289     Accepted: 2160
    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
    Source
    
    Duke Internet Programming Contest 1993,uva 124

    理解为 建AOV图,找所有的拓扑序列,dfs实现

    这个输入真的是,非常,难受。

    大坑点是输入的变量列表不一定是有序的,要先sort一下。

    每组输出之间还有一个空行。

    一直卡 迷茫..

    #include<iostream>
    #include<string>
    #include<cstring>
    #include<algorithm>
    #define MAXN 20000
    using namespace std;
    
    string r;
    int lenr;
    
    int in[3000];
    struct Edge {
        char to;
        int next;
    } e[MAXN];
    int ecnt,head[MAXN];
    inline void add(char x,char y) {
        e[++ecnt].to = y;
        e[ecnt].next = head[x];
        head[x]=ecnt;
    }
    
    char ans[MAXN];
    bool vis[MAXN];
    void print() {
        for(int i=1;i<=lenr;i++) printf("%c",ans[i]);
        printf("
    ");
    //  for(int i=1; i<=lenr; i++) cout<<ans[i];//<<" ";
    //  cout<<endl;
    }
    void dfs(int dp) {
        if(dp==lenr+1) {
            print();
            return;
        }
        for(int i=0; i<lenr; i++) {
            int c=r[i];
            if(!vis[c]&&in[c]==0) {
                for(int j=head[c]; j!=-1; j=e[j].next) {
                    in[e[j].to]--;
                }
                vis[c]=1;
                ans[dp]=c;
                dfs(dp+1);
                for(int j=head[c]; j!=-1; j=e[j].next) {
                    in[e[j].to]++;
                }
                vis[c]=0;
            }
        }
    }
    
    
    int main() {
    //  freopen("output.txt","w",stdout);
        string s,ss;
    
        while(getline(cin,ss)) {
    
            char rr[100000];
            if(ss=="") return 0;
            memset(head,-1,sizeof(head));
            memset(vis,0,sizeof(vis));
            memset(in,0,sizeof(in));
            memset(e,0,sizeof(e));
            memset(rr,0,sizeof(rr));
            r=s="";//!!
            ecnt=0;
            int lens=ss.size() ;
            int p=0;
            for(int i=0;i<lens;i++) if(ss[i]!=' ') rr[p++]=ss[i];
            rr[p]='';//!!
    
            sort(rr,rr+strlen(rr)-1);
            r=rr;
            lenr=r.size() ;
    //      cout<<r<<endl;
    //      gets(ss.data() );
            getline(cin,ss);
            lens=ss.size();
            for(int i=0;i<lens;i++) if(ss[i]!=' ') s.push_back(ss[i]); 
            s.push_back(''); 
    //      cout<<r<<endl<<s<<endl;
            lens=s.size();
            for(int i=0; i<lens; i+=2) {
                add(s[i],s[i+1]);
                in[s[i+1]]++;
            }
            dfs(1);
            cout<<endl;
        }
        return 0;
    }
    

    本文来自博客园,作者:GhostCai,转载请注明原文链接:https://www.cnblogs.com/ghostcai/p/9247519.html

  • 相关阅读:
    java环境的配置
    java基本语法
    QTP的那些事QTP回放iFrame控件时间非常慢的问题分析
    QTP的那些事有关xml的操作函数
    QTP的那些事xpath的使用(转)
    QTP的那些事有关web的自动化测试框架saffron的使用
    selenium的那些事命令行启动selenium并运行测试(转)
    QTP的那些事XPath的重要使用
    QTP的那些事有关report manager的使用
    测试环境服务器windows server 2003资源下载
  • 原文地址:https://www.cnblogs.com/ghostcai/p/9247519.html
Copyright © 2011-2022 走看看