您的位置:首页 > 其它

(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代码:

#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: