首页 > 代码库 > POJ 1270 Following Orders

POJ 1270 Following Orders

Following Orders
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 4902 Accepted: 1982

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

Source

Duke Internet Programming Contest 1993,uva 124
——————————————————我是分割线——————————————————————————————
这题就是一个给定部分顺序,来确定整体顺序的拓扑排序。
但一般的拓扑排序只找出一种符合要求的序列,这题要求找出所有符合要求的序列,这就有点尴尬,
所以还得加上回溯算法。最后对求出的所有符合要求的序列进行排序输出就可以了。
(顺便练习一下sstream)
技术分享
 1 /* 2     Problem: 3     OJ: 4     User:    S.B.S. 5     Time: 6     Memory: 7     Length: 8 */ 9 #include<iostream>10 #include<cstdio>11 #include<cstring>12 #include<cmath>13 #include<algorithm>14 #include<sstream>15 #include<queue>16 #include<cstdlib>17 #include<iomanip>18 #include<cassert>19 #include<climits>20 #include<functional>21 #include<bitset>22 #include<vector>23 #include<list>24 #include<map>25 #define F(i,j,k) for(int i=j;i<=k;i++)26 #define M(a,b) memset(a,b,sizeof(a))27 #define FF(i,j,k) for(int i=j;i>=k;i--)28 #define maxn 1000129 #define inf 0x3f3f3f3f30 #define maxm 100131 #define mod 99824435332 //#define LOCAL33 using namespace std;34 int read(){35     int x=0,f=1;char ch=getchar();36     while(ch<0||ch>9){if(ch==-)f=-1;ch=getchar();}37     while(ch>=0&&ch<=9){x=x*10+ch-0;ch=getchar();}38     return x*f;39 }40 int n,m;41 int a[maxn],d[maxn];42 int pos[maxn],cnt[maxn][2];43 bool vis[maxn];44 inline void dfs(int u)45 {46     if(u>n){47         F(i,1,n) cout<<(char)a[i];48         cout<<endl;49         return;50     }51     F(i,1,n){52         if(!vis[i]){53             a[u]=d[i];54             pos[a[u]]=u;55             vis[i]=true;56             bool flag=true;57             for(int j=1;j<=m&&flag;j++)58             {59                 int aa=cnt[j][0],bb=cnt[j][1];60                 if(pos[aa]==0||pos[bb]==0||pos[aa]<pos[bb]);61                 else flag=false;62             }63             if(flag) dfs(u+1);64             pos[a[u]]=0;65             vis[i]=false;66         }67     }68 }69 int main()70 {71 //    std::ios::sync_with_stdio(false);//cout<<setiosflags(ios::fixed)<<setprecision(1)<<y;72     #ifdef LOCAL73     freopen("data.in","r",stdin);74     freopen("data.out","w",stdout);75     #endif76     string s;77     istringstream ss;78     char aa,bb,cc;79     while(getline(cin,s))80     {81         M(vis,0);M(pos,0);82         n=m=0;ss.clear();83         ss.str(s);84         while(ss>>cc) d[++n]=cc;85         sort(d+1,d+n+1);86         getline(cin,s);87         ss.clear();88         ss.str(s);89         while(ss>>aa>>bb){90             cnt[++m][0]=aa;91             cnt[m][1]=bb;92         }93         dfs(1);94         cout<<endl;95     }96     return 0;97 }
poj 1270

 

POJ 1270 Following Orders