poj 1270 Following Orders (简单的拓扑排序)

来源:互联网 发布:语音识别模块 淘宝 编辑:程序博客网 时间:2024/05/22 14:14
Following Orders
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 3123 Accepted: 1194

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

abfgabgfagbfgabfwxzvywzxvyxwzvyxzwvyzwxvyzxwvy

Source

Duke Internet Programming Contest 1993,uva 124

题意:不说了

思路:拓扑排序   dfs  从小往大搜  可以保证先搜出来的结果字典顺序小

感想:小错误一大堆   还是以前犯过的错误   

1.dfs时用来临时存储的数组设置成了全局变量    X…X

2.vector忘记清0了     ╮(╯▽╰)╭

代码:

#include <iostream>#include <cstdio>#include <cstring>#include <vector>#define maxn 30using namespace std;int n,m,ans,len;bool vis[maxn];int a[maxn],num[maxn];char s1[1005],s2[1005],s[maxn];vector<int>v[maxn];void dfs(int k,int pos){    int i,j,sz,tnum[maxn];    // tnum 一定不能设置成全局变量  debug好久。。。    s[pos]='a'+k;    if(pos>=len)    {        s[pos+1]='\0';        printf("%s\n",s);        return ;    }    memcpy(tnum,num,sizeof(num));    sz=v[k].size();    for(i=0; i<sz; i++)    {        num[v[k][i]]--;    }    for(i=0; i<26; i++)    {        if(a[i]&&!num[i]&&!vis[i])        {            vis[i]=1;            dfs(i,pos+1);            vis[i]=0;        }    }    memcpy(num,tnum,sizeof(num));}int main(){    int i,j,l1,l2,tt=-1;    while(gets(s1)!=NULL)    {        tt++;        gets(s2);        l1=strlen(s1);        l2=strlen(s2);        len=l1/2;        memset(a,0,sizeof(a));        memset(num,0,sizeof(num));        memset(vis,0,sizeof(vis));        if(tt) printf("\n");        for(i=0; i<l1; i+=2)        {            a[s1[i]-'a']=1;            v[s1[i]-'a'].clear();      // 开始忘记清0了 贡献了几次WA X…X          }        for(i=0; i<l2; i+=4)        {            v[s2[i]-'a'].push_back(s2[i+2]-'a');            num[s2[i+2]-'a']++;        }        for(i=0; i<26; i++)        {            if(a[i]&&!num[i])            {                vis[i]=1;                dfs(i,0);                vis[i]=0;            }        }    }    return 0;}


原创粉丝点击