Following orders
Time limit:1000 ms |
|
Memory limit:10000 K |
Total submissions:3040 |
|
Accepted:1160 |
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 writing 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
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 shocould 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
This question can be set to an edge smaller than the link, but different from other Directed Graphs, because less than signs are passed, for example, if a <B, B <C, A <C. therefore, when creating the edge, you need to add this point (this was ignored at the beginning, and the result was debug For A Long Time). The other is the topological sorting problem.
# Include <iostream> # include <algorithm> # include <cstring> # include <cstdio> using namespace STD; int map [30] [30]; int phosphatase [30], CNT; // count the occurrence of the letter int ans [30]; int visit [30]; void DFS (int I) {if (I = CNT) {for (Int J = 0; j <CNT; j ++) printf ("% C", ANS [J] + 'A '); printf ("\ n"); return;} For (Int J = 0; j <CNT; j ++) {If (visit [phosphatase [J]) continue; ans [I] = phosphatase [J]; bool state = true; For (int K = I; k> 0 & State; k --) for (INT L = 0; L <K; l ++) if (Map [Ans [k] [ans [l]) {state = false; break;} If (! State) continue; visit [Kp [J] = 1; DFS (I + 1); visit [Kp [J] = 0 ;}} int main () {char s [50], CST [300], C; int I, J, K, Case = 0; while (gets (s) {gets (Cst ); memset (MAP, 0, sizeof (MAP); memset (visit, 0, sizeof (visit); I = 0; CNT = 0; while (C = s [I ++])! = '\ 0') {If (C = '') continue; alkaline [CNT ++] = C-'A';} I = 0; char C1, C2; while (CST [I]! = '\ 0') {If (CST [I] = '') I ++; C1 = CST [I ++]; if (CST [I] = '') I ++; C2 = CST [I ++]; map [C1-'a'] [C2-'a'] = 1;} // use the pass-through property smaller than signs for (I = 0; I <CNT; I ++) for (j = 0; j <CNT; j ++) for (k = 0; k <CNT; k ++) if (Map [phosphatase [I] [phosphatase [J] & map [phosphatase [J] [phosphatase [k]) map [alkaline process [I] [alkaline process [k] = 1; sort (alkaline process + CNT); If (Case ++) printf ("\ n "); DFS (0);} return 0 ;}