POJ 1270 Following Orders
来源:互联网 发布:现在淘宝搜索规则 编辑:程序博客网 时间:2024/06/05 10:23
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.
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.
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.
Output for different constraint specifications is separated by a blank line.
Sample Input
a b f ga b b fv w x y zv y x v z v w v
Sample Output
abfgabgfagbfgabfwxzvywzxvyxwzvyxzwvyzwxvyzxwvy
方法:dfs+拓扑排序
//By Sean Chen#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>#include <map>using namespace std;char str[105];int Map[30][30],cnt1,cnt,indegree[30],flag[30],used[30];char ans[35];void dfs(int step) //dfs结合拓扑排序{ if (step==cnt1) { printf("%s\n",ans); return; } for (int i=0;i<26;i++) { if (indegree[i]==0 && flag[i] && !used[i]) { indegree[i]=-1; ans[step]=i+'a'; used[i]=1; for (int j=0;j<26;j++) { if (Map[i][j]) indegree[j]--; } dfs(step+1); used[i]=0; for (int j=0;j<26;j++) { if (Map[i][j]) indegree[j]++; } indegree[i]=0; } } return;}int main(){ while (gets(str)) { memset(flag,0,sizeof(flag)); memset(indegree,0,sizeof(indegree)); memset(Map,0,sizeof(Map)); int l=strlen(str); cnt1=0; for (int i=0;i<l;i++) { if (str[i]!=' ') { flag[str[i]-'a']=1; cnt1++; } } /*for (int i=0;i<26;i++) printf("%d",flag[i]);*/ gets(str); l=strlen(str); cnt=0; for (int i=0;i<l;i++) { if (str[i]!=' ') { if (cnt%2) { int temp1=str[i-2]-'a',temp2=str[i]-'a'; indegree[temp2]++; Map[temp1][temp2]=1; } cnt++; } } memset(ans,'\0',sizeof(ans)); memset(used,0,sizeof(used)); ans[cnt1]='\0'; dfs(0); printf("\n"); } return 0;}
0 0
- POJ 1270 Following Orders
- poj 1270 Following Orders
- POJ 1270 Following Orders
- POJ-1270 Following Orders
- POJ 1270 Following Orders
- POJ 1270 Following Orders
- poj 1270 Following Orders
- poj 1270 Following Orders
- poj 1270 Following Orders
- POJ 1270 Following Orders
- POJ 1270 Following Orders
- POJ 1270 Following Orders
- poj 1270 Following Orders 拓扑
- POJ 1270 Following Orders 笔记
- POJ 1270 Following Orders (拓扑排序)
- POJ 1270 HOJ 1170 Following Orders
- POJ 1270 Following Orders 拓扑排序
- POJ 1270 Following Orders 拓扑排序
- 设置开机启动
- java中的JDBC创建步骤--自我总结,我的首篇勿喷...
- 400. Nth Digit
- 【Python】学习笔记——-19、访问数据库
- SeekBar样式修改
- POJ 1270 Following Orders
- Maven实战(四)——基于Maven的持续集成实践
- 第一篇
- Android勒索病毒分析报告-王者荣耀刷皮肤
- iBatis简介及其不适用场合
- python学习(一)常见的数据结构
- 背景色的范围
- 接口的实现
- linux开发环境搭建(1)-配置网卡