码迷,mamicode.com
首页 > Windows程序 > 详细

poj1270Following Orders(拓扑排序+dfs回溯)

时间:2014-08-09 11:35:47      阅读:302      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   os   io   strong   

题目链接:

题意是:
第一列给出所有的字母数,第二列给出一些先后顺序。然后按字典序最小的方式输出所有的可能性。。。
思路:
总体来说是拓扑排序,但是又很多细节要考虑,首先要按字典序最小的方式输出,所以自然输入后要对这些字母进行排列,然后就是输入了,用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\n",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("\n");
    }
    return 0;
}


poj1270Following Orders(拓扑排序+dfs回溯),布布扣,bubuko.com

poj1270Following Orders(拓扑排序+dfs回溯)

标签:des   style   blog   http   color   os   io   strong   

原文地址:http://blog.csdn.net/u014303647/article/details/38454955

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!