(POJ 1270)Following Orders 求序列在限制条件下的 [全排列]
2017-10-13 17:40
176 查看
Following Orders
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 5218 Accepted: 2126
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
Source
Duke Internet Programming Contest 1993,uva 124
题意:
给你一些字符,并有一些限制条件:x y 表示x 要在 y的前面出现。
问你满足条件的所有排列方式,按字典序依次输出。
分析:
问题的实质就是在求全排列的基础上,加上一些限制条件。
所以整体上是在求全排列,然后根据限制条件去掉一些不满足条件的排列即可。
关于求全排列: 直接dfs就可以了
关于去掉不满足的排列: 我们用ans[]来存当前的排列,并依次填入字符。
在填入第cur个字符y时,若在第0~cur-1字符中存在x,有限制条件 y x (y要出现在x前面),则该排列与限制条件矛盾,剪枝掉。
AC代码:
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 5218 Accepted: 2126
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
Source
Duke Internet Programming Contest 1993,uva 124
题意:
给你一些字符,并有一些限制条件:x y 表示x 要在 y的前面出现。
问你满足条件的所有排列方式,按字典序依次输出。
分析:
问题的实质就是在求全排列的基础上,加上一些限制条件。
所以整体上是在求全排列,然后根据限制条件去掉一些不满足条件的排列即可。
关于求全排列: 直接dfs就可以了
关于去掉不满足的排列: 我们用ans[]来存当前的排列,并依次填入字符。
在填入第cur个字符y时,若在第0~cur-1字符中存在x,有限制条件 y x (y要出现在x前面),则该排列与限制条件矛盾,剪枝掉。
AC代码:
#include <iostream> #include <cstdio> #include <cstring> #include <string> #include <algorithm> #include <cmath> #include <queue> using namespace std; char vars[64],cons[256],ans[64]; int g[128][128],vis[256]; int len; void dfs(int cur) { if(cur == len) { puts(ans); return; } for(int i=0;i<len;i++) { if(vis[vars[i]] == 0) { int f = 1; for(int j=0;j<cur && f;j++) //判断是否和前cur-1个字符矛盾 { if(g[vars[i]][ans[j]]) f = 0; } if(f == 0) continue; ans[cur] = vars[i]; vis[vars[i]] = 1; dfs(cur + 1); vis[vars[i]] = 0; //回溯 } } } int main() { while(gets(vars)) { memset(g,0,sizeof(g)); memset(vis,0,sizeof(vis)); gets(cons); for(int i=0;cons[i];i+=4) { g[cons[i]][cons[i+2]] = 1; } len = 0; for(int i=0;vars[i];i++) { if(vars[i]>='a' && vars[i]<='z') vars[len++] = vars[i]; } vars[len] = '\0'; ans[len] = '\0'; sort(vars,vars+len); dfs(0); puts(""); } return 0; }
相关文章推荐
- poj 1724 ROADS条件限制下的最短路径
- poj 2085 Inversion 已知数个数为n,逆序数为ver,求最小满足条件的排列
- poj 1724 有限制条件的最短路
- poj 1724 ROADS条件限制下的最短路径
- poj 1724 ROADS条件限制下的最短路径
- 带有限制条件的第K个排列——CF 513B1、B2
- poj 1724 ROADS条件限制下的最短路径
- POJ - 1724【有限制条件的最短路】+HDU- 3790【双重权值的最短路】
- POJ 1192 限制条件最短路
- poj 1724 ROADS条件限制下的最短路径
- poj 2888 Magic Bracelet 有限制条件的(有限制条件的polay问题)
- (POJ 3687)Labeling Balls [逆top序列] 求按照某种排列方式的字典序最小的top序列
- Exercise(18):排列(有限制条件)
- zzuliOJ 1919:分割序列(二分+限制条件判断)
- 【codeforces 691 E】【矩阵快速幂 思维题】【给定序列,从序列中选择k个数(可以重复选择),使得得到的排列满足xi与xi+1异或的二进制表示中1的个数是3的倍数。问满足条件的序列个数】
- 带有限制条件的数字排列
- POJ 1062 有限制条件的最短路
- POJ 2288 Islands and Bridges(状压DP->多种限制条件的TSP)
- poj 1724 ROADS条件限制下的最短路径
- poj 1724 有限制条件的最短路