poj1270 Following Orders (拓扑排序+回溯输出路径)
2016-07-19 20:35
309 查看
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.
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. 最后面还要输出一个换行
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 4756 | Accepted: 1909 |
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; }
相关文章推荐
- Java烧脑驴游(十)--String类
- for嵌套循环"大圈套小圈"思想
- linux常用的一些命令(一)
- 前索尼高管:推荐游戏和获取投资的14个技巧
- SVN:重命名文件之后不允许提交
- 文件管理 内部存储 外部存储 Sdcard
- Dubins路径及航迹跟踪控制
- El表达式
- LeetCode Guess Number Higher or Lower
- angular用ng-repeat批量处理数据
- UVA-152 Tree's a Crowd
- codeforces 651a oysticks
- 题目1043:Day of Week
- python多线程(2)--thread和threading模块的使用
- [置顶] * **ACM错误总结
- 杂物
- Red Hat Linux9命令行--修改补充中
- 5招教你避免野指针
- 【杭电-oj】-1070-Milk(结构体)
- UVA-10474 Where is the Marble?