http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=60
http://poj.org/problem?id=1270
Following Orders
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 |
题意:
输入有两行,第一行给出若干出现的字母,第二行给出若干对关系x y,表示x<y,要从小到大排序,求所有合法序列,按字典序输出。
分析:
按字典序输出所有的拓扑序,和POJ 1128 & ZOJ 1083的方法一样,回溯求解即可,详情请戳这里: POJ 1128 & ZOJ 1083 Frame Stacking (拓扑排序)
这题的输入是比较恶心的,要注意写得鲁棒些。
/* * * Author : fcbruce * * Date : 2014-08-10 15:22:35 * */ #include <cstdio> #include <iostream> #include <cstdlib> #include <algorithm> #include <ctime> #include <cctype> #include <cmath> #include <string> #include <cstring> #include <stack> #include <queue> #include <list> #include <vector> #include <map> #include <set> #define sqr(x) ((x)*(x)) #define LL long long #define itn int #define INF 0x3f3f3f3f #define PI 3.1415926535897932384626 #define eps 1e-10 #define maxm #define maxn using namespace std; bool appear[30],pending[30],G[30][30]; int deg[30],st[30]; int n; void write() { for (int i=0;i<n;i++) putchar(st[i]+'a'); putchar ( '\n'); } void toposort(int x,int top) { st[++top]=x; if (top+1==n) { write(); return ; } for (int i=0;i<30;i++) { if (G[x][i]) { deg[i]--; if (deg[i]==0) { pending[i]=true; } } } for (int i=0;i<30;i++) { if (pending[i]) { pending[i]=false; toposort(i,top); pending[i]=true; } } for (int i=0;i<30;i++) { if (G[x][i]) { deg[i]++; if (deg[i]!=0) { pending[i]=false; } } } } void slove() { memset(pending,0,sizeof pending); for (int i=0;i<30;i++) { if (appear[i] && deg[i]==0) pending[i]=true; } for (int i=0;i<30;i++) { if (pending[i]) { pending[i]=false; toposort(i,-1); pending[i]=true; } } } int main() { #ifndef ONLINE_JUDGE freopen("/home/fcbruce/文档/code/t","r",stdin); #endif // ONLINE_JUDGE char last,ch; int cnt=0; bool first=true,cmp=false; memset(appear,0,sizeof appear); memset(G,0,sizeof G); memset(deg,0,sizeof deg); while ( (ch=getchar())!=EOF) { if (ch==' ') continue; if (ch=='\n') { cnt++; } if (cnt==0) { appear[ch-'a']=true; n++; } if (cnt==1) { if (ch=='\n') continue; if (cmp) { cmp=false; if (G[last-'a'][ch-'a']) continue; G[last-'a'][ch-'a']=true; deg[ch-'a']++; } else { cmp=true; last=ch; } } if (cnt==2) { if (!first) putchar( '\n'); first=false; slove(); memset(appear,0,sizeof appear); memset(G,0,sizeof G); memset(deg,0,sizeof deg); cnt=n=0; } } return 0; }