回溯法

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

abfg
abgf
agbf
gabf

wxzvy
wzxvy
xwzvy
xzwvy
zwxvy
zxwvy

我的代码

依旧是全排列的题,只不过其中有些元素之间有严格的先后关系。那么只需要在搜索时,检查当前的这一步是否满足操作就行了。具体实现是:记vector cons[i]为i的前置元素,例如对于每一对限制(x,y),x就是y的前置元素。那么在搜索时,若当前需要部署元素j,就需要检查cons[j]中每一个前置元素是否剩余个数都为0,如果不满足则不可以部署。

#include<stdio.h>
#include<string.h>
#include<vector>

using namespace std;


char v[100];
char c[210];
int num[30];
int ans[30];
int len;
vector<int> cons[26];

void parse(){
	int len1=strlen(v);
	int len2=strlen(c);
	char temp[110];
	int tag=0;
	len=0;
	for (int i=0;i<len1;i++){
		if (v[i]!=' '){
			num[v[i]-'a']++;
			len++;
		}
	}
	for (int i=0;i<len2;i++){
		if (c[i]!=' '){
			temp[tag++]=c[i];
		}
	}
	for (int i=0;i+1<tag;i=i+2){
		cons[temp[i+1]-'a'].push_back(temp[i]-'a');
	}
}

void traceback(){
	for (int i=0;i<len;i++){
		printf("%c",ans[i]+'a');
	}
	printf("\n");
}

void dfs(int step){
	if (step==len+1){
		traceback();
	}else{
		for (int i=0;i<26;i++){
			if (num[i]>0){
				int flag=1;
				for (int j=0;j<cons[i].size();j++){
					if (num[cons[i][j]]!=0){
						flag=0;
						break;
					}
				}
				if (flag==1){
					num[i]--;
					ans[step-1]=i;
					dfs(step+1);
					num[i]++;
				}
			}
		}
	}
}

int main()
{
	
	while (gets(v)!=0)
	{
		gets(c);
		memset(num,0,sizeof(num));
		for (int i=0;i<26;i++){
			cons[i].clear();
		}
		parse();
		for (int i=0;i<26;i++){
			if (num[i]>0){
				int flag=1;
				for (int j=0;j<cons[i].size();j++){
					if (num[cons[i][j]]!=0){
						flag=0;
						break;
					}
				}
				if (flag==1){
					num[i]--;
					ans[0]=i;
					dfs(2);
					num[i]++;
				}
			}
		}
		printf("\n");//Presentation Error
	}
	return 0;
}

反思

又忘了输出空行了。

发表评论