[POJ] 1270 Following Orders

Following Orders
Time Limit: 1000MS      Memory Limit: 10000K
Total Submissions: 5289     Accepted: 2160
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

理解为 建AOV图,找所有的拓扑序列,dfs实现

这个输入真的是,非常,难受。

大坑点是输入的变量列表不一定是有序的,要先sort一下。

每组输出之间还有一个空行。

一直卡 迷茫..

#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
#define MAXN 20000
using namespace std;

string r;
int lenr;

int in[3000];
struct Edge {
    char to;
    int next;
} e[MAXN];
int ecnt,head[MAXN];
inline void add(char x,char y) {
    e[++ecnt].to = y;
    e[ecnt].next = head[x];
    head[x]=ecnt;
}

char ans[MAXN];
bool vis[MAXN];
void print() {
    for(int i=1;i<=lenr;i++) printf("%c",ans[i]);
    printf("\n");
//  for(int i=1; i<=lenr; i++) cout<<ans[i];//<<" ";
//  cout<<endl;
}
void dfs(int dp) {
    if(dp==lenr+1) {
        print();
        return;
    }
    for(int i=0; i<lenr; i++) {
        int c=r[i];
        if(!vis[c]&&in[c]==0) {
            for(int j=head[c]; j!=-1; j=e[j].next) {
                in[e[j].to]--;
            }
            vis[c]=1;
            ans[dp]=c;
            dfs(dp+1);
            for(int j=head[c]; j!=-1; j=e[j].next) {
                in[e[j].to]++;
            }
            vis[c]=0;
        }
    }
}


int main() {
//  freopen("output.txt","w",stdout);
    string s,ss;

    while(getline(cin,ss)) {

        char rr[100000];
        if(ss=="") return 0;
        memset(head,-1,sizeof(head));
        memset(vis,0,sizeof(vis));
        memset(in,0,sizeof(in));
        memset(e,0,sizeof(e));
        memset(rr,0,sizeof(rr));
        r=s="";//!!
        ecnt=0;
        int lens=ss.size() ;
        int p=0;
        for(int i=0;i<lens;i++) if(ss[i]!=' ') rr[p++]=ss[i];
        rr[p]='\0';//!!

        sort(rr,rr+strlen(rr)-1);
        r=rr;
        lenr=r.size() ;
//      cout<<r<<endl;
//      gets(ss.data() );
        getline(cin,ss);
        lens=ss.size();
        for(int i=0;i<lens;i++) if(ss[i]!=' ') s.push_back(ss[i]); 
        s.push_back('\0'); 
//      cout<<r<<endl<<s<<endl;
        lens=s.size();
        for(int i=0; i<lens; i+=2) {
            add(s[i],s[i+1]);
            in[s[i+1]]++;
        }
        dfs(1);
        cout<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值