poj 1270 Following Orders

Following Orders
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 2194   Accepted: 794

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
//=========================================================//zoj_1270//DFS+拓扑 or floyd + next_permutation//=========================================================
#include<iostream>
using namespace std;
#include<string>
#include<algorithm>
char str1[100],str2[100];
bool rel[30][30];
int pos[30];
void floyd()//传递闭包
{
	int i,j,k;
	for(k=0;k<26;k++)
		for(i=0;i<26;i++)
			for(j=0;j<26;j++)
			{
				if(rel[i][k]&&rel[k][j])
					rel[i][j]=true;
			}
}
int chk(char *str)
{
	int i;
	for(i=0;i<26;i++)
		for(int j=0;j<26;j++)
			if(rel[i][j] && pos[i]>pos[j])// 如果字母i+'a',j+'a'存在关系,但是i+'a'位置比j+'a'后就false
				return false;
			return true;
}
int main()
{
	//freopen("a.txt","r",stdin);
	while(gets(str1) && strlen(str1)!=0)
	{
		
		int len1=strlen(str1);
		//if(len1==0)return 0;
		gets(str2);
		int len2=strlen(str2);
		int cnt=0;
		int i;
		for(i=0;i<len1;i++)
		{
			if(str1[i]>='a' && str1[i]<='z')
				str1[cnt++]=str1[i];
		}
		str1[cnt]='/0';
		memset(rel,false,sizeof(rel));
		for(i=0;i<len2;i+=4)
		{
			//if(i+2<len2)
			rel[str2[i]-'a'][str2[i+2]-'a']=true;
		}
		sort(str1,str1+cnt);//要先排序!!wa了一次
		floyd();
		do
		{
			for(i=0;i<cnt;i++)
				pos[str1[i]-'a']=i;//记录每个的排列的每个字母位置
			if(chk(str1))
			printf("%s/n",str1);
		}while(next_permutation(str1,str1+cnt));
		printf("/n");
	}
	return 0;
}
 

你可能感兴趣的:(list,input,constraints,character,permutation,variables)