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 题意:给出一些字母 , 和字母间关系 ,让你输出所有符合关系的排序分析:相当于是按字典序输出所有的拓扑排序情况。 鉴于要输出所有情况,那么我在拓扑排序时给每个字母设置一个等级,在用next_permutation找到所有排列,只要复合拓扑排序规则,这个排列一定是等级递增关系,直接输出。本以为这样做会T,没想到数据太弱,0msAC。
1 #include<cstring> 2 #include<cstdio> 3 #include<iostream> 4 #include<algorithm> 5 #include<queue> 6 #include<map> 7 #include<vector> 8 #include<cstdlib> 9 10 using namespace std; 11 12 char a[105],b[105]; 13 int vis[105]; 14 int in[105]; 15 vector<int>out[105]; 16 vector<int>chat; 17 vector<int>temp; 18 19 void init() 20 { 21 for(int i=0;i<26;i++) 22 { 23 in[i]=0; 24 out[i].clear(); 25 vis[i]=-2; 26 } 27 chat.clear(); 28 temp.clear(); 29 } 30 31 void toposort() 32 { 33 int num=0; 34 int cur=0; 35 for(int i=0;i<chat.size();i++) if(vis[chat[i]]==-2) num++; 36 37 while(num) 38 { 39 40 for(int i=0;i<chat.size();i++) 41 { 42 if(in[chat[i]]==0&&vis[chat[i]]!=-1) 43 { 44 in[chat[i]]--; 45 temp.push_back(chat[i]); 46 } 47 } 48 49 for(int i=0;i<temp.size();i++) 50 { 51 int k=temp[i]; 52 vis[k]=cur; 53 for(int j=0;j<out[k].size();j++) 54 { 55 in[out[k][j]]--; 56 } 57 } 58 59 num-=temp.size(); 60 temp.clear(); 61 cur++; 62 63 } 64 } 65 66 bool judge() 67 { 68 int maxn=0; 69 for(int i=0;i<chat.size();i++) 70 { 71 if(vis[chat[i]]!=-1) 72 { 73 if(vis[chat[i]]>=maxn) maxn=vis[chat[i]]; 74 else return false; 75 } 76 } 77 return true; 78 } 79 80 int main() 81 { 82 int black=1; 83 while(gets(a)!=NULL) 84 { 85 gets(b); 86 87 if(black==0) printf("\n"); 88 init(); 89 90 int len=strlen(a); 91 for(int i=0;i<len;i++) 92 { 93 if(a[i]>=‘a‘&&a[i]<=‘z‘) 94 { 95 chat.push_back(a[i]-‘a‘); 96 } 97 } 98 99 len=strlen(b); 100 char k=‘#‘; 101 for(int i=0;i<len;i++) 102 { 103 if(b[i]>=‘a‘&&b[i]<=‘z‘) 104 { 105 if(k==‘#‘) k=b[i]; 106 else 107 { 108 in[b[i]-‘a‘]++; 109 out[k-‘a‘].push_back(b[i]-‘a‘); 110 k=‘#‘; 111 } 112 } 113 } 114 115 for(int i=0;i<chat.size();i++) 116 if(!in[chat[i]]&&!out[chat[i]].size()) vis[chat[i]]=-1; 117 118 toposort(); 119 120 sort(chat.begin(),chat.end()); 121 do 122 { 123 if(judge()) 124 { 125 for(int i=0;i<chat.size();i++) 126 printf("%c",chat[i]+‘a‘); 127 puts(""); 128 } 129 }while(next_permutation(chat.begin(),chat.end())); 130 131 black=0; 132 } 133 }