poj1270 Following Orders

Following Orders
Time Limit: 1000MSMemory Limit: 10000K
Total Submissions: 3040Accepted: 1160

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
 
     这题可以按照小于关系建边,但与其它有向图不同的是,由于小于号具有传递性,比如说若a<b,b<c则可推出a<c.所以在建边时,需将这点补充进去(刚开始将这点忽视了,结果debug了很久啊)其它的就是拓扑排序问题了。
 
#include <iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>

using namespace std;
int map[30][30];
int alp[30],cnt;//统计出现的字母
int ans[30];
int visit[30];

void dfs(int i)
{
    if(i==cnt)
    {
        for(int j=0;j<cnt;j++)
            printf("%c",ans[j]+'a');
        printf("\n");
        return;
    }
    for(int j=0;j<cnt;j++)
    {
        if(visit[alp[j]])
            continue;
        ans[i]=alp[j];
        bool state=true;
        for(int k=i;k>0&&state;k--)
            for(int l=0;l<k;l++)
                if(map[ans[k]][ans[l]])
                {
                    state=false;
                    break;
                }
        if(!state)
            continue;
        visit[alp[j]]=1;
        dfs(i+1);
        visit[alp[j]]=0;
    }
}

int main()
{
    char s[50],cst[300],c;
    int i,j,k,Case=0;
    while(gets(s))
    {
        gets(cst);
        memset(map,0,sizeof(map));
        memset(visit,0,sizeof(visit));
        i=0;
        cnt=0;
        while((c=s[i++])!='\0')
        {
            if(c==' ')
                continue;
            alp[cnt++]=c-'a';
        }
        i=0;
        char c1,c2;
        while(cst[i]!='\0')
        {
            if(cst[i]==' ')
                i++;
            c1=cst[i++];
            if(cst[i]==' ')
                i++;
            c2=cst[i++];
            map[c1-'a'][c2-'a']=1;
        }
        //利用小于号的传递性
        for(i=0;i<cnt;i++)
            for(j=0;j<cnt;j++)
                for(k=0;k<cnt;k++)
                    if(map[alp[i]][alp[j]]&&map[alp[j]][alp[k]])
                        map[alp[i]][alp[k]]=1;
        sort(alp,alp+cnt);
        if(Case++)
            printf("\n");
        dfs(0);
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值