poj 1270 Following Orders

Following Orders
Time Limit: 1000MS        Memory Limit: 10000K
Total Submissions: 2732        Accepted: 1041

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
Duke Internet Programming Contest 1993,uva 124

// 拓扑排序思想 dfs输出所有情况
#include <iostream>
#include <stdio.h>
#include <queue>
#include <stack>
#include <set>
#include <vector>
#include <math.h>
#include <string.h>
#include <algorithm>
using namespace std;
vector <int> v[22];
char ch[22];
int  id[124];
int  in[22];
bool br[22];
char pt[22];
int  n;
void dfs(int dp)
{
    if(dp==n) {pt[dp]='\0';printf("%s\n",pt);return;}
    int i,l,k;
    for(i=0;i<n;i++)
    {
        if(br[i]) continue;
        if(in[i]==0)
        {
            pt[dp]=ch[i];
            br[i]=1;
            l=v[i].size();
            for(k=0;k<l;k++)
              in[v[i][k]]--;
            dfs(dp+1);
            br[i]=0;
             for(k=0;k<l;k++)
              in[v[i][k]]++;
        }
    }
}
int main()
{
    int f=0;
    char a,b;
    int i,k;
    while(scanf("%c",&a)!=EOF)
    {
         n=0;
        memset(in,0,sizeof(in));
        memset(br,0,sizeof(br));
        ch[n++]=a;
        while(scanf("%c",&a),a!='\n')
        {
            if(a!=' ')
            {
                ch[n++]=a;
            }
        }
       // printf("%d",n);
        sort(ch,ch+n);
        for(i=0;i<n;i++)
        {
            id[ch[i]]=i;
           // printf("%c",ch[i]);
        }
        while(scanf("%c %c",&a,&b))
        {
            v[id[a]].push_back(id[b]);
            in[id[b]]++;
           // printf("%c %c",a,b);
            if(scanf("%c",&a)==EOF)
               return 0;
           // printf("%c",a);
             if(a=='\n') break;
        }
        if(f) printf("\n"); else f=1;
        dfs(0);
        for(i=0;i<n;i++) v[i].clear();
    }
    return 0;
}
 

posted on 2012-11-12 14:45  江财小子  阅读(182)  评论(0编辑  收藏  举报