• 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
    

    解题思路:一个一个的添,当填满的时候输出,注意添的时候,新添的一个必须没被用过,并且前面没比他小的.     比如第二行输入时a b;代表a在b前面,则填入a时,前面不能有b;


     

    #include <iostream>
    #include <string>
    using namespace std;
    const int maxn=30; 
    const int maxm=500; 
    int n;
    int G[maxn][maxn]; 
    int vis[maxn]; 
    int ans[maxn]; 
    int cnt; 
    bool mark[maxn];

    bool ok(int i,int cnt)
    {int j;
     for(j=0;j<cnt;j++)
    if(G[i][ans[j]])return 0;
    return 1;


    }

    void dfs(int cnt)
    {int i,j;
     if(cnt==n)
    {for(j=0;j<n;j++)
    cout<<char(ans[j]+'a');
    cout<<endl;}
    else{
    for(i=0;i<26;i++)
    if(mark[i]&&!vis[i]&&ok(i,cnt))
    {vis[i]=1;
    ans[cnt]=i;
    dfs(cnt+1);
    vis[i]=0;
    }
    }
    }
    int main()
    {
    char str[1000];
    while(gets(str))//注意输入时有空格;

    {int i;

     n=0; 
     memset(mark,0,sizeof(mark)); 
     memset(G,0,sizeof(G)); 
     memset(vis,0,sizeof(vis));
    for(i=0;i<str[i];i++)if(str[i]!=' ') 
    {mark[str[i]-'a']=true;
    n++;}
    gets(str);
    for( i=0;str[i];i++)if(str[i]!=' ') 
            { 
                int a,b; 
                a=str[i++]-'a'; 
                while(str[i]==' ') 
                    i++; 
                b=str[i]-'a'; 
                G[a][b]=1; 
            } 


    dfs(0);

    cout<<endl;//注意每次输出后都有一空行
    }
    return 0;}

  • 相关阅读:
    学习Mybatis与mysql数据库的示例笔记
    SpringAOP学习笔记
    idea开发ssh(Spring+struts+Hibernate)实现对MySQL数据库的增删改查
    springmvc加vue实现前后端数据的跨域访问
    idea开发工具springmvc加vue.js实现MySQL数据库的查询操作
    利用idea开发工具实现ssh(spring+struts+hibernate)加vue.js前后台对数据库的查询
    appweb 7.0.2版本编译
    Unable to register the DLL/OCX: RegSvr32 failed with exit code 0x3 我的解决方法
    无法定位程序输入点 InitializeCriticalSectionEx 于动态链接库 Kernel32.dll 上 问题解决方法
    海思3516D + IMX291图像闪烁问题定位
  • 原文地址:https://www.cnblogs.com/lengxia/p/4387804.html
Copyright © 2020-2023  润新知