Following Orders
Time Limit: 1000MS
Memory Limit: 10000K
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
思路
拓扑排序经典题,对图用dfs扫一遍,并并删除度数为零的顶点和出边,之后运用回溯找出所有的序列即可,用C++和Java各写了一遍。附上C++和java的代码:
代码Java版
import java.util.*;
import java.math.*;
import java.io.*;
public class Main {
static int N;
static int[] pre;
static boolean[] has;
static String var,v;
public static void print(String x) {
System.out.print(x);
}
static void dfs(int dep,String res)
{
if(dep==N+1)
{
print(res+"\n");
return;
}
for(int i='a';i<='z';++i)
{
if(has[i]&&pre[i]==0)
{
has[i]=false;
for(int k=0;k<v.length();k+=4)
if(v.charAt(k)==i)
--pre[v.charAt(k+2)];
dfs(dep+1,res+(char)i);
for(int k=0;k<v.length();k+=4)
if(v.charAt(k)==i)++pre[v.charAt(k+2)];
has[i]=true;
}
}
}
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
while(input.hasNextLine())
{
var=input.nextLine();
v=input.nextLine();
has=new boolean[1<<8];
for(int i=0;i<var.length();i+=2)
has[var.charAt(i)]=true;
N=var.length()/2+1;
pre=new int[1<<8];
for(int i=0;i<v.length();i+=4)
++pre[v.charAt(i+2)];
dfs(1,"");
print("\n");
}
}
}
代码C++版
#include<iostream>
#include<cstring>
#include<string>
using namespace std;
const int maxn = 200;
string var, v;
int pre[maxn], N;
bool has[maxn];
void dfs(int dep, string str)
{
if (dep == N) {
cout << str << endl;
return;
}
int i, j;
for (i = 'a'; i <= 'z'; ++i) {
if (has[i] && pre[i] == 0) {
for (j = 0; j < (int)v.length(); j = j + 4) {
if (v.at(j) == i)
--pre[v.at(j + 2)];
}
has[i] = false;
dfs(dep + 1, str + (char)i);
for (j = 0; j < (int)v.length(); j = j + 4) {
if (v.at(j) == i)
++pre[v.at(j + 2)];
}
has[i] = true;
}
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int i;
while (getline(cin, var, '\n')) {
getline(cin, v, '\n');
memset(has, 0, sizeof has);
memset(pre, 0, sizeof pre);
N = 0;
for (i = 0; i < (int)var.length(); i = i + 2) {
has[var.at(i)] = true;
N++;
}
for (i = 2; i < (int)v.length(); i = i + 4)
++pre[v.at(i)];
dfs(0, "");
cout << endl;
}
return 0;
}
/*
a b f g
a b b f
v w x y z
v y x v z v w v
*/