zoukankan      html  css  js  c++  java
  • poj1270 Following Orders

    
    

    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 #include<cstdio>
     2 #include<cstring>
     3 #include<algorithm>
     4 
     5 using namespace std;
     6 
     7 int t[26],in[26],cnt;//t[26]用来存放输出序列,in[26]用来存放入度,cnt用来记录输入的变量个数
     8 char ch,str[26];//str[26]用来存放变量
     9 bool G[26][26];//用来存放有向图
    10 
    11 int sert(char ch)//查找ch在str中的位置
    12 {
    13     int i;
    14     for(i=0;i<cnt;i++)
    15         if(str[i]==ch)  return i;//返回位置
    16         return -1;//没找到
    17 }
    18  
    19 bool read()//输入处理
    20 {
    21     int u,v;
    22     cnt =0;
    23     while((ch=getchar())!='\n'){
    24         if(ch==EOF) return false;
    25         if(ch!=' ') str[cnt++]=ch;
    26     }
    27     sort(str,str+cnt);
    28     memset(in,0,sizeof(in));
    29     memset(G,false,sizeof(G));
    30     while(1){
    31         while((ch=getchar())==' ');//跳掉空格
    32         if(ch==EOF) return false;
    33         if(ch=='\n')  break;
    34         u=sert(ch);
    35         while((ch=getchar())==' ');//跳掉空格
    36         v=sert(ch);
    37         if(G[u][v]) continue;
    38         G[u][v]= true;
    39         in[v]++;
    40     }
    41     return true;
    42 }
    43 void dfs(int cur)
    44 {
    45     int i,j;
    46     if(cur==cnt) {
    47         for(i=0;i<cnt;i++)
    48             printf("%c",str[t[i]]);
    49         printf("\n");
    50     }
    51 
    52     for(i=0;i<cnt;i++){
    53         if(in[i]==0){
    54             in[i]=-1;
    55             t[cur]=i;
    56             for(j=0;j<cnt;j++)
    57                 if(G[i][j]) in[j]--;
    58                 dfs(cur+1);
    59             in[i]=0;
    60             for(j=0;j<cnt;j++)
    61                 if(G[i][j]) in[j]++;
    62         }
    63     }
    64 }
    65 int main()
    66 {
    67     while(read())
    68     {
    69         dfs(0);
    70         printf("\n");
    71     }
    72     return 0;
    73 }
     
  • 相关阅读:
    一些文件的扩展名
    关于git,从svn转到git
    trousers--------dpkg: 处理软件包 trousers (--configure)时报错
    Ubuntu下运行DrClient以上网
    Ubuntu下的终端命令--复制文件从一个文件夹到另一个文件夹下
    VSCode放大字体的快捷键
    opessl版本过低造成的函数使用错误
    python的基本语法
    ubuntu和windows的解码方式
    ubuntu下强制删除文件夹
  • 原文地址:https://www.cnblogs.com/xiaofanke/p/2743177.html
Copyright © 2011-2022 走看看