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

    题目链接:

    题意是:
    第一列给出全部的字母数,第二列给出一些先后顺序。

    然后按字典序最小的方式输出全部的可能性。。


    思路:
    整体来说是拓扑排序。可是又非常多细节要考虑。首先要按字典序最小的方式输出,所以自然输入后要对这些字母进行排列。然后就是输入了。用scanf不能读空格。所以怎么建图呢??设置一个变量推断读入的先后顺序。那么建图完成后,就拓扑排序了。那么多种方式自然就是dfs回溯了。。那么这个问题就得到了解决。。


    题目:

    Following Orders
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 3800   Accepted: 1502

    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




    代码为:

    #include<cstdio>
    #include<iostream>
    #include<algorithm>
    #include<map>
    #include<cstring>
    using namespace std;
    
    const int maxn=26+2;
    char str[maxn],ans[maxn],apa[maxn];
    int in[maxn],gra[maxn][maxn],res;
    map<char,int>mp;
    
    void topo(int depth)
    {
        if(depth==res)
        {
            printf("%s
    ",ans);
            return;
        }
        for(int i=0;i<res;i++)
        {
            if(in[i]==0)
            {
                --in[i];
                ans[depth]=apa[i];
                for(int j=0;j<res;j++)
                {
                    if(gra[i][j])
                       --in[j];
                }
                topo(depth+1);
                ++in[i];
                for(int j=0;j<res;j++)
                {
                    if(gra[i][j])
                       ++in[j];
                }
            }
        }
    }
    
    int main()
    {
        int flag,k,len;
        char temp1,temp2;
        while(gets(str))
        {
            k=0;
            memset(ans,0,sizeof(ans));
            memset(in,0,sizeof(in));
            memset(gra,0,sizeof(gra));
            len=strlen(str);
            for(int i=0;i<len;i++)
               if(str[i]>='a'&&str[i]<='z')
                    apa[k++]=str[i];
            sort(apa,apa+k);
            for(int i=0;i<k;i++)
                mp[apa[i]]=i;
            res=k;
            gets(str);
            len=strlen(str);
            for(int i=0;i<len;i++)
                if(str[i]>='a'&&str[i]<='z')
            {
                if(flag)
                {
                    temp1=str[i];
                    flag=0;
                }
                else
                {
                    temp2=str[i];
                    gra[mp[temp1]][mp[temp2]]=1;
                    in[mp[temp2]]++;
                    flag=1;
                }
            }
           topo(0);
           printf("
    ");
        }
        return 0;
    }


  • 相关阅读:
    【09-04】java内部类学习笔记
    【09-03】java泛型学习笔记
    【08-23】redis学习笔记
    【06-23】js动画学习笔记01
    【11-23】mysql学习笔记02
    【06-18】CentOS使用笔记
    thinkphp+datatables+ajax 大量数据服务器端查询
    python遗传算法实现数据拟合
    简单的新闻客户端APP开发(DCloud+thinkphp+scrapy)
    python游戏编程——跟13岁儿童学编程
  • 原文地址:https://www.cnblogs.com/yjbjingcha/p/6848760.html
Copyright © 2011-2022 走看看