Following Orders
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 2717   Accepted: 1037

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
// 代码一:TNND 一直WA ,就是不知道哪错了。。。
#include<cstring>
#include<cstdio>
#include<cctype>
#include<algorithm>

using namespace std;

int cnt;
char n[30];
int d[30],ans[30];
int G[30][30];

int search(char ch)
{
    for(int i=0;i<cnt;++i)
        if(n[i]==ch)
            return i;
    return -1;
}

bool readData()
{
    char ch;
    cnt=0;
    memset(d,0,sizeof(d));
    memset(G,0,sizeof(G));
    /*
    while((ch=getchar())!='\n')
    {
        if(ch==EOF)
            return false;
        if(isalpha(ch))
            n[cnt++]=ch;
    }
    n[cnt]='\0';
    sort(n,n+cnt);
    while(true)
    {
        while(!isalpha(ch=getchar()));
        int i=search(ch);
        while(!isalpha(ch=getchar()));
        int j=search(ch);
        if(G[i][j]==0)
        {
            G[i][j]=1;
            ++d[j];
        }
        if((ch=getchar())=='\n')
            return true;
        else if(ch==EOF)
            return false;
    }
    */
    char s1[220],s2[550];
    while(gets(s1))
    {
        int len=strlen(s1);
        for(int i=0;i<len;i+=2)
            n[cnt++]=s1[i];
        n[cnt]='\0';
        gets(s2);
        len=strlen(s2);
        for(int i=0;i<len;i+=4)
        {
            int x=search(s2[i]);
            int y=search(s2[i+2]);
            if(!G[x][y])
            {
                G[x][y]=1;
                ++d[y];
            }
        }
        return true;
    }
    return false;
}

void DFS(int cur)
{
    if(cur==cnt)
    {
        for(int i=0;i<cnt;++i)
            printf("%c",n[ans[i]]);
        printf("\n");
        return;
    }
    for(int i=0;i<cnt;++i)
    {
        if(d[i]==0)
        {
            d[i]=-1;
            ans[cur]=i;
            for(int j=0;j<cnt;++j)
            {
                if(G[i][j])
                    --d[j];
            }
            DFS(cur+1);
            d[i]=0;                 //回溯
            for(int j=0;j<cnt;++j)
            {
                if(G[i][j])
                    ++d[j];
            }
        }
    }
}

int main()
{
    while(readData())
    {
        DFS(0);
        printf("\n");
    }
    return 0;
}


//代码二:---copy明明哥的AC
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

int cnt, len;
char s[30];
int in[30], A[30];
bool G[30][30];

int search(int ch)
{
	for (int i = 0; i < cnt; ++i)
		if (s[i] == ch) return i;
	return -1;
}

bool readData(void)
{
	int ch;

	cnt = 0;
	while ((ch = getchar()) != '\n') {
		if (ch == EOF) return false;
		if (ch != ' ') s[cnt++] = ch;
	}
	s[cnt] = '\0';
	sort(s, s+cnt);

	memset(in, 0, sizeof(in));
	memset(G, false, sizeof(G));
	while (true) {
		while ((ch = getchar()) == ' ');
		if (ch == '\n') break;
		if (ch == EOF) return false;
		int u = search(ch);
		while ((ch = getchar()) == ' ');
		int v = search(ch);
		if (G[u][v]) continue;
		G[u][v] = true;
		in[v]++;
	}

	return true;
}

void dfs(int cur)
{
	if (cur == cnt) {
		for (int i = 0; i < cnt; ++i)
			printf("%c", s[A[i]]);
		printf("\n");
		return;
	}

	for (int i = 0; i < cnt; ++i) {
		if (in[i] == 0) {
			in[i] = -1;
			A[cur] = i;
			for (int j = 0; j < cnt; ++j)
				if (G[i][j]) in[j]--;
			dfs(cur+1);
			in[i] = 0;
			for (int j = 0; j < cnt; ++j)
				if (G[i][j]) in[j]++;
		}
	}
}

int main()
{
	while (readData()) {
		dfs(0);
		printf("\n");
	}

	return 0;
}

/*
//代码三:网上copy的
//思路: floyd(传递闭包) + STL应用。

#include <iostream>
#include <algorithm>

using namespace std;

const int MAXN = 26;
const int MAXM = 300;
int l, ll, pos[MAXN];
bool rel[MAXN][MAXN];  //relation

void init(char *mes)
{
      int len = strlen(mes);
      ll = 0;
      for(int i = 0; i < len; i += 2)
        mes[ll ++] = mes[i];
      mes[ll] = '\0';
}
void floyd()
{
      for(int k = 0; k < 26; k ++)
            for(int i = 0; i < 26; i ++)
                 for(int j = 0; j < 26; j ++)
                      if(rel[i][k] && rel[k][j]) rel[i][j] = true;
}
bool check()
{
      for(int i = 0; i < 26; i ++)
            for(int j = 0; j < 26; j ++)
                 if(rel[i][j] && pos[i] > pos[j]) return false;
      return true;
}
int main()
{
      char mes[MAXM], str[MAXM];
      while(gets(mes)) {
            gets(str);
            init(mes);
            l = strlen(str);
            memset(rel, false, sizeof(rel));
            for(int i = 0; i < l; i += 4) {
                  rel[str[i] - 'a'][str[i + 2] - 'a'] = true;
                  floyd();
            }
            sort(mes, mes + ll);
            do {
                  for(int j = 0; j < ll; j ++) pos[mes[j] - 'a'] = j;
                  if(check()) printf("%s\n", mes);
            }while(next_permutation(mes, mes + ll));
            printf("\n");
      }
      return(0);
}
*/

  




posted on 2012-10-28 15:39  可笑痴狂  阅读(569)  评论(0编辑  收藏  举报