zoukankan      html  css  js  c++  java
  • poj1270 Following Orders(拓扑排序+dfs回溯)

    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 5783   Accepted: 2386

    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

    题目大意:
    多组数据输入,每组数据两行,第一行为出现的字母,第二行为字母之间的关系,成对出现,前者小于后者
    求满足的拓扑排序,存在多种排序,按字典序升序输出

    思路:
    在dfs的基础上用拓扑排序

     1 #include <iostream>
     2 #include <stdio.h>
     3 #include <cstring>
     4 #include <set>
     5 using namespace std;
     6 
     7 const int N=30;
     8 char s[50],str[150];
     9 int side,x,y,is[N],maps[N][N],vis[N];
    10 set<string> st;
    11 
    12 void init(){
    13     st.clear();
    14     memset(is,0,sizeof is);
    15     memset(maps,0,sizeof maps);
    16     memset(vis,0,sizeof vis);
    17 }
    18 
    19 int check(int x,string res,int cnt){
    20     int flag=1;
    21     for(int i=0;i<cnt;i++){
    22         if(maps[x][res[i]-'a']==1){
    23             flag=0;
    24             break;
    25         }
    26     }
    27     return flag;
    28 }
    29 
    30 void dfs(int cnt,string res){
    31     if(cnt==side){
    32         st.insert(res);
    33     }
    34     else{
    35         for(int i=0;i<N;i++){
    36             if(is[i]==1&&vis[i]==0&&check(i,res,cnt)){
    37                 string cmp=" ";
    38                 cmp[0]='a'+i;
    39                 vis[i]=1;
    40                 dfs(cnt+1,res+cmp);
    41                 vis[i]=0;
    42             }
    43         }
    44     }
    45 }
    46 
    47 int main(){
    48     int ca=0;
    49     while(gets(s)){
    50         init();
    51         gets(str);
    52         int len=strlen(s);
    53         for(int i=0;i<len;i+=2){
    54             is[s[i]-'a']=1;
    55         }
    56         side=(len+1)/2;
    57         len=strlen(str);
    58         for(int i=0;i<len;i+=4){
    59             x=str[i]-'a',y=str[i+2]-'a';
    60             maps[x][y]=1;
    61         }
    62         string res="";
    63         dfs(0,res);
    64         if(ca)cout<<endl;
    65         ca++;
    66         for(set<string>::iterator it=st.begin();it!=st.end();it++){
    67             cout<<*it<<endl;
    68         }
    69     }
    70 }
  • 相关阅读:
    一、模板的渲染
    十六、ajax上传图片 mvc
    十五、API请求接口-远程服务器返回错误: (400) 错误的请求错误
    九、操作(在结果中搜索+查询该表,单独几个字段查询,通过子查询方式查询)
    一、Vue分页实现
    【2019-10-29】除非你变成你自己
    【2019-10-28】美好在于细节心态
    【2019-10-27】理智是罗盘,感情是大风
    【2019-10-26】日积月累的小惠生者
    【2019-10-25】从错误和失败中获得经验
  • 原文地址:https://www.cnblogs.com/ChangeG1824/p/11663767.html
Copyright © 2011-2022 走看看