POJ 1270 Following Orders

来源:互联网 发布:现在淘宝搜索规则 编辑:程序博客网 时间:2024/06/05 10:23

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 ga b b fv w x y zv y x v z v w v

Sample Output

abfgabgfagbfgabfwxzvywzxvyxwzvyxzwvyzwxvyzxwvy

方法:dfs+拓扑排序

//By Sean Chen#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>#include <map>using namespace std;char str[105];int Map[30][30],cnt1,cnt,indegree[30],flag[30],used[30];char ans[35];void dfs(int step)               //dfs结合拓扑排序{    if (step==cnt1)    {        printf("%s\n",ans);        return;    }    for (int i=0;i<26;i++)    {        if (indegree[i]==0 && flag[i] && !used[i])        {            indegree[i]=-1;            ans[step]=i+'a';            used[i]=1;            for (int j=0;j<26;j++)            {                if (Map[i][j])                    indegree[j]--;            }            dfs(step+1);            used[i]=0;            for (int j=0;j<26;j++)            {                if (Map[i][j])                    indegree[j]++;            }            indegree[i]=0;        }    }    return;}int main(){    while (gets(str))    {        memset(flag,0,sizeof(flag));        memset(indegree,0,sizeof(indegree));        memset(Map,0,sizeof(Map));        int l=strlen(str);        cnt1=0;        for (int i=0;i<l;i++)        {            if (str[i]!=' ')            {                flag[str[i]-'a']=1;                cnt1++;            }        }        /*for (int i=0;i<26;i++)            printf("%d",flag[i]);*/        gets(str);        l=strlen(str);        cnt=0;        for (int i=0;i<l;i++)        {            if (str[i]!=' ')            {                if (cnt%2)                {                    int temp1=str[i-2]-'a',temp2=str[i]-'a';                    indegree[temp2]++;                    Map[temp1][temp2]=1;                }                cnt++;            }        }        memset(ans,'\0',sizeof(ans));        memset(used,0,sizeof(used));        ans[cnt1]='\0';        dfs(0);        printf("\n");    }    return 0;}

0 0
原创粉丝点击