A - Following Orders(11.3.1))

来源:互联网 发布:mac地址绑定错误 编辑:程序博客网 时间:2024/06/05 15:11
A - Following Orders(11.3.1))
Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u
Submit Status

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 ga b b fv w x y zv y x v z v w v

Sample Output

题目大意:给定一组变量和及其形式为x<y的约束,请写一个程序,把所有的约束一致的变量次序输出。例如,给出约束x<y,x<z,那么x,y,z三个变量就可以构成两个满足该约束的有序集:x y z和x z y.

此题可利用拓扑排序输出相应序列。

选用删边法及采用DFS方法

由于每一条拓扑子路径的首节点入度为0,因此可采用如下方法:

1,从图中选择一个入度为0的节点且输出之;

2,从图中删除该节点及其所有出边(即与之相邻的节点入度减一)。

反复执行这两个步骤直至所有节点都输出。

代码:

#include <iostream>#include <cstring>#include <string>#include <cstdio>using namespace std;char var[1000],v[1000];//var为变量串,v为约束条件串bool has[1000];int pre[1000];//节点的度数int len1,len2;//dep为节点数量,res为形成的输出序列void dfs(int dep,string res){    int j;    if(dep==(len1/2)+1)//所有节点入度为0输出该序列    {        cout<<res<<endl;        return ;    }    char i;    for(i='a';i<='z';i++)//按字典序搜索每一个入度为0的点    {        if(has[i]&&(pre[i]==0))//如果该节点没有被访问过,并且入度为0则标记        {            has[i]=false;            for(j=0;j<len2;j+=4)//搜索他的所有相邻节点并使其入度减一            if(v[j]==i)                pre[v[j+2]]--;               dfs(dep+1,res+i);//递归搜索下一个入度为0的点            for(j=0;j<len2;j+=4)//恢复递归前的状态                 if(v[j]==i)                {                    pre[v[j+2]]++;                }            has[i]=true;        }    }}int main(){    int i,j;    while(gets(var))    {        memset(pre,0,sizeof(pre));        memset(has,0,sizeof(has));        len1=strlen(var);        for(i=0;i<len1;i+=2)//对输入节点做未访问标记            has[var[i]]=true;        gets(v);        len2=strlen(v);        for(j=0;j<len2;j+=4)//计算各节点的入度            pre[v[j+2]]++;            dfs(0,"");            cout<<endl;    }    return 0;}


0 0
原创粉丝点击