您的位置:首页 > 其它

poj1270 Following Orders (拓扑排序+回溯输出路径)

2016-07-19 20:35 309 查看
Following Orders

Time Limit: 1000MSMemory Limit: 10000K
Total Submissions: 4756Accepted: 1909
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.

InputThe 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.

OutputFor 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 Inputa b f g

a b b f

v w x y z

v y x v z v w vSample Outputabfg

abgf

agbf

gabf

wxzvy

wzxvy

xwzvy

xzwvy

zwxvy

zxwvy

1. 最后的时候是因为top-1和fl弄错了,WA,因为是一个不可显字符,所以本机看不出来

2. 特别要注意按字典序,所以一开始要对char数组排序,所以我们得到的才会是字典序

3. 输入处理用gets,gets碰到EOF读取到的是NULL,所以我们直接while就行了

4. 最后面还要输出一个换行


#include<iostream>
using namespace std;
#include<vector>
#include<queue>
#include<cstring>
#include<cstdio>
#include<map>
#include<algorithm>

vector<int> vec[50];
int deg[50];
int top;
int path[50];
int pl;
int nums[50];
char var[50];

void dfs(int fl) {
if(fl==top-1) {
for(int i=0; i<fl; ++i) {
printf("%c",var[path[i]]);
}
putchar(10);
return ;
}
for(int i=1; i<top; ++i) {
if(!deg[i]) {
--deg[i];
int len=vec[i].size();
for(int j=0; j<len; ++j) {
int to=vec[i][j];
--deg[to];
}
path[fl]=i;
dfs(fl+1);
++deg[i];
for(int j=0; j<len; ++j) {
int to=vec[i][j];
++deg[to];
}
}
}
}
int main() {
char in[30];
#ifdef tangge
freopen("1270.txt","r",stdin);
#endif // tangge

while(gets(in)) {
int len=strlen(in);
sort(in,in+len);
top=1;
for(int i=0; i<len; ++i) {
if(in[i]!=' ') {
nums[in[i]-'a']=top;
var[top]=in[i];
++top;
}
}
gets(in);
len=strlen(in);
int newl=0;
for(int i=0; i<len; ++i) {
if(in[i]!=' ')in[newl++]=in[i];
}
in[newl]='\0';
for(int i=0; i<50; ++i)
vec[i].clear(),deg[i]=0;
for(int i=0; i<newl; i+=2) {
vec[nums[in[i]-'a']].push_back(nums[in[i+1]-'a']);
++deg[nums[in[i+1]-'a']];
}
dfs(0);
putchar(10);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: