POJ1270--Following Orders

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

abfgabgfagbfgabfwxzvywzxvyxwzvyxzwvyzwxvyzxwvy

/*
每次从入度为0的字母中选最小的,然后将其接着的那个字母的
入度-1。。有接着,所以需要个next数组
还需要个存入度的数组
还需要个存答案的数组
至多20个变量 50个约束 
*/
#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
using namespace std;
#define maxn 28
int vis[maxn];//用来标记是否有出现这个字母
int Next[maxn];//用来存这个字母的下一个字母是谁
int rudu[maxn];//用来存这个字母的入度
int ans[maxn];
int n = 0;
vector <int> Ans[28];
void dfs(int deep)
{
	if(deep == n+1)
	{
		for(int i=1;i<deep;i++)
		{
			char f = ans[i]+'a'-1;
			printf("%c",f);
		}
		printf("\n");
		return;
	}
	for(int i=1;i<=26;i++)
	{
		if(vis[i] && rudu[i]==0)
		{
			for(int j=0;j<Ans[i].size();j++)
			{
				rudu[Ans[i][j]]--;
			}
			vis[i]=0;
			ans[deep] = i;
			dfs(deep+1);
			vis[i]=1;
			for(int j=0;j<Ans[i].size();j++)
			{
				rudu[Ans[i][j]]++;
			}
		}
	}
}
int main()
{
	char c;
	while(cin>>c)
	{
		n = 1;
		memset(vis,0,sizeof(vis));
		memset(rudu,0,sizeof(rudu));
		memset(Next,0,sizeof(Next));
		vis[c-'a'+1] = 1;
		getchar();
		for(int i=1;i<=27;i++) Ans[i].clear();
		while((c=getchar())!='\n')
		{
			n++;
			vis[c-'a'+1] = 1;
			if((c=getchar())=='\n')break;
		}
		int k=0;
		char a,b;
		while((c=getchar())!='\n')
		{
			k++;
			if(k&1) a=c-'a'+1;
			else 
			{
				b=c-'a'+1;
				rudu[b]++;
				Ans[a].push_back(b);
			}
			if((c=getchar())=='\n') break;
		}
		dfs(1);
		printf("\n");
	}
	return 0;
}


 

/*
方法2:
用next_permutation生成全排列后判断是否符合。
*/
#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std;
#define maxn 28
int pos[maxn];
char str[28];
int ee;
//读入字符后用排序,然后用next-permutation生成全排列.然后判断
struct Edge
{
	int from,to;
}edge[58];
bool Judge()
{
	bool flag = true;
	for(int i=1;i<ee;i++)
	{
		if(pos[edge[i].from] >= pos[edge[i].to])
		{
			return 0;
		}
	}
	return 1;
}
int main()
{
	char c;
	while(cin>>c)
	{
		int n = 1;
		str[0] = c;
		getchar();
		while((c=getchar())!='\n')
		{
			n++;
			str[n-1] = c;
			if((c=getchar())=='\n') break;
		}
		str[n]='\0';
		char a,b;
		int k = 0;
		ee=1;
		while((c=getchar())!='\n')
		{
			k++;
			if(k&1) a = c-'a'+1;
			else 
			{
				b = c-'a'+1;
				edge[ee].from = a;
				edge[ee++].to = b;
			}
			if((c=getchar())=='\n') break;
		}
		sort(str,str+n);
		for(int i=0;i<n;i++)
		{
			pos[str[i]-'a'+1] = i+1;
		}
		if(Judge()) printf("%s\n",str);
		while(next_permutation(str,str+n))
		{
			for(int i=0;i<n;i++)
			{
				pos[str[i]-'a'+1] = i+1;
			}
			if(Judge()) printf("%s\n",str);
		}
		printf("\n");
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值