POJ 1270 Follwing Orders

poj 100题留影合照

Following Orders
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 4305 Accepted: 1726

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

[Submit]   [Go Back]   [Status]   [Discuss]

ACcode:
#include <map>
#include <queue>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#define rd(x) scanf("%d",&x)
#define rd2(x,y) scanf("%d%d",&x,&y)
#define ll long long int
#define maxn 100005
#define mod 1000000007
#define INF 0x3f3f3f3f //int×î´óÖµ
#define FOR(i,f_start,f_end) for(int i=f_start;i<=f_end;++i)
#define MT(x,i) memset(x,i,sizeof(x))
#define PI  acos(-1.0)
#define E  exp(1)
using namespace std;
string str,temp;
bool has[1256];
int pre[256],n,pos;
char res[maxn];
void dfs(int dep,string res){
    if(dep==n+1){
        cout<<res<<'\12';
        return ;
    }
    for(int i=97;i<=122;i++)
        if(has[i]&&pre[i]==0){
            has[i]=false;
            for(int k=0;k<temp.length();k+=4)
                if(temp[k]==i)--pre[temp[k+2]];
            dfs(dep+1,res+(char)i);
            for(int k=0;k<temp.length();k+=4)
                if(temp[k]==i)++pre[temp[k+2]];
            has[i]=true;
        }
}
int main(){
    while(getline(cin,str)){
        MT(pre,0);
        MT(has,0);
        for(int i=0;i<maxn;++i)res[i]='\0';
        for(int i=0;i<str.length();i+=2)
            has[str[i]]=true;
        getline(cin,temp);
        n=str.length()/2+1;
        for(int i=0;i<temp.length();i+=4)
            ++pre[temp[i+2]];
        dfs(1,"");
        printf("\n");
    }
    return 0;
}
/**
a b f g
a b b f
v w x y z
v y x v z v w v
**/



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值