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 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
题意:第一行表示给出的字母,第二行每两个一组(x,y)表示x<y,要你把这行字母的顺序打出来,不唯一就按字典序打
思路:先把各自关系放在一个表里,然后按顺序dfs。因为每次都会判断后面的元素会不会大于前面的,所以矛盾的就不会输出。
代码:
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<queue>
#include<cmath>
#include<string>
#include<stack>
#include<set>
#include<map>
#include<vector>
#include<iostream>
#include<algorithm>
#include<sstream>
#define ll long long
const int N=35;
const int INF=1e9;
using namespace std;
int is[N],vis[N],mp[N][N],ans[N],size;
int can(int x,int cnt){
for(int i=0;i<cnt;i++){
if(mp[ans[i]][x]) return 0; //i大于x,x不能放i后面
}
return 1;
}
void dfs(int cnt){
if(cnt==size){
for(int i=0;i<size;i++){
printf("%c",ans[i]+'a');
}
cout<<endl;
return;
}
for(int i=0;i<26;i++){
if(is[i] && !vis[i] && can(i,cnt)){
vis[i]=1;
ans[cnt]=i;
dfs(cnt+1);
vis[i]=0;
}
}
}
int main(){
char s[1000];
while(gets(s)){
memset(is,0,sizeof(is));
memset(vis,0,sizeof(vis));
memset(mp,0,sizeof(mp));
int len=strlen(s);
size=0;
for(int i=0;i<len;i++){
if(s[i]!=' '){
is[s[i]-'a']=1;
size++;
}
}
gets(s);
len=strlen(s);
for(int i=0;i<len;i++){
if(s[i]!=' '){
int a=s[i++]-'a';
while(s[i]==' ') i++;
int b=s[i]-'a';
mp[b][a]=1; //a<b,表示b大于a存在
}
}
dfs(0);
cout<<endl;
}
return 0;
}