Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 5783 | Accepted: 2386 |
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.
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.
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.
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
题目大意:
多组数据输入,每组数据两行,第一行为出现的字母,第二行为字母之间的关系,成对出现,前者小于后者
求满足的拓扑排序,存在多种排序,按字典序升序输出
思路:
在dfs的基础上用拓扑排序
1 #include <iostream> 2 #include <stdio.h> 3 #include <cstring> 4 #include <set> 5 using namespace std; 6 7 const int N=30; 8 char s[50],str[150]; 9 int side,x,y,is[N],maps[N][N],vis[N]; 10 set<string> st; 11 12 void init(){ 13 st.clear(); 14 memset(is,0,sizeof is); 15 memset(maps,0,sizeof maps); 16 memset(vis,0,sizeof vis); 17 } 18 19 int check(int x,string res,int cnt){ 20 int flag=1; 21 for(int i=0;i<cnt;i++){ 22 if(maps[x][res[i]-'a']==1){ 23 flag=0; 24 break; 25 } 26 } 27 return flag; 28 } 29 30 void dfs(int cnt,string res){ 31 if(cnt==side){ 32 st.insert(res); 33 } 34 else{ 35 for(int i=0;i<N;i++){ 36 if(is[i]==1&&vis[i]==0&&check(i,res,cnt)){ 37 string cmp=" "; 38 cmp[0]='a'+i; 39 vis[i]=1; 40 dfs(cnt+1,res+cmp); 41 vis[i]=0; 42 } 43 } 44 } 45 } 46 47 int main(){ 48 int ca=0; 49 while(gets(s)){ 50 init(); 51 gets(str); 52 int len=strlen(s); 53 for(int i=0;i<len;i+=2){ 54 is[s[i]-'a']=1; 55 } 56 side=(len+1)/2; 57 len=strlen(str); 58 for(int i=0;i<len;i+=4){ 59 x=str[i]-'a',y=str[i+2]-'a'; 60 maps[x][y]=1; 61 } 62 string res=""; 63 dfs(0,res); 64 if(ca)cout<<endl; 65 ca++; 66 for(set<string>::iterator it=st.begin();it!=st.end();it++){ 67 cout<<*it<<endl; 68 } 69 } 70 }