POJ - 1270 Following Orders(拓扑排序+dfs回溯)

该博客介绍了如何利用拓扑排序和深度优先搜索(DFS)解决POJ 1270问题。博主探讨了在给定字母及其相互关系的情况下,如何生成并按字典序输出所有可能的排序方案。

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

题意 :你有一定的字母和他们之间的关系 ,你把所有可能的排序按字典序输出

#include<stdio.h>
#include<string>
#include<string.h>
#include<math.h>
#include<iostream>
#include<algorithm>
#include<map>
#include<vector>
#include<queue>
using namespace std;
char s1[500],s2[500];//输入的字符串
int p[30];//邻接表
int a[30];//入度个数
int k[30];//存输出的字符
int v[30];//回溯中的标记
int ans[30];//dfs记录要输出的东西
int tot,cnt;
struct node//邻接表
{
    int en,next;
}e[10000];
void init()
{
    memset(p,-1,sizeof(p));
    memset(a,0,sizeof(a));
    memset(v,0,sizeof(v));
    tot=0;
    cnt=0;
}
void add(int u,int v)
{
    e[tot].en=v;
    e[tot].next=p[u];
    p[u]=tot++;
}
int dfs(int cur)
{
    if(cur==cnt)//如果到了cnt个,输出
    {
        for(int i=0;i<cnt;i++)
            printf("%c",ans[i]+'a');
        printf("\n");
    }
    for(int i=0;i<cnt;i++)
    {
        if(!v[k[i]]&&!a[k[i]])//如果未被标记而且,入度为0
        {
            ans[cur]=k[i];//记录
            v[k[i]]=1;//标记
            for(int j=p[k[i]];j+1;j=e[j].next)//每条边连向的点入度--
                a[e[j].en]--;
            dfs(cur+1);//更深一层
            v[k[i]]=0;//取消标记
            for(int j=p[k[i]];j+1;j=e[j].next)
                a[e[j].en]++;
        }
    }
}
int main()
{
   while(gets(s1))
   {
       gets(s2);
       init();
       int t1=strlen(s1);
       int t2=strlen(s2);
       for(int i=0;i<t1;i+=2)
       {
           if(s1[i]>='a'&&s1[i]<='z')
               k[cnt++]=s1[i]-'a';
       }
       sort(k,k+cnt);
       for(int i=0;i<t2;i+=4)
       {
           if(s2[i]>='a'&&s2[i]<='z')
           {
               int u=s2[i]-'a';
               int v=s2[i+2]-'a';
               add(u,v);
               a[v]++;
           }
       }
       dfs(0);
       printf("\n");
   }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值