POJ1270 Following Orders(字符串处理+拓扑序全排列)

题目链接

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.

1.题目意思很好懂,输入几个字母,给出字母之间的限制条件,进行拓扑排序。但是是按字典序输出所有拓扑序列

2.刚开始不会做,看题解也很懵,完全不知道是什么意思。直到看到一篇博客讲这是dfs全排列的思想。我去学了一下全排列,恍然大悟!
但是字符串处理真的很头疼,我使用了两个map,一个用来ID化字符,另一个用来输出时ID和字符转化,处理起来挺麻烦的

代码:

#include <iostream>
#include <vector>
#include <sstream>
#include <cstring>
#include <string>
#include <cstdio>
#include <map>
#include <algorithm>
using namespace std;
const int maxn=500;
vector<int> G[30];
int in[30];
int vis[30];
char ans[maxn];
int n,tot;
string s1,s2;
map<int,char> mp1;
map<char,int> mp2;

void dfs_topsort(int x){
    if(x==tot){
        for(int i=0;i<n;i++) printf("%c",ans[i]);
        printf("\n");
        return;
    }
    for(int i=0;i<n;i++){
        if(!vis[i]&&!in[i]){
            ans[x]=mp1[i];
            vis[i]=1;
            for(int j=0;j<G[i].size();j++)
                in[G[i][j]]--;
            dfs_topsort(x+1);
            vis[i]=0;
            for(int j=0;j<G[i].size();j++) in[G[i][j]]++; //比dfs求全排列多了个恢复入度的过程
        }
    }

}

int main()
{
    int flag=0;
    while(getline(cin,s1)){
        getline(cin,s2);
        memset(vis,0,sizeof vis);
        memset(in,0,sizeof in);
        mp1.clear();mp2.clear();
        string str="";
        stringstream sss(s1);
        stringstream ss(s2);
        char c1,c2;
        while(sss>>c1){
            str+=c1;
        }
        sort(str.begin(),str.end());  //因为要按字典序输出因此要先排序
        n=tot=str.size();
        for(int i=0;i<n;i++){
            mp1[i]=str[i];
            mp2[str[i]]=i;
        }
        while(ss>>c1>>c2){
            G[mp2[c1]].push_back(mp2[c2]);
            in[mp2[c2]]++;
        }
        if(flag++) printf("\n");
        dfs_topsort(0);
        for(int i=0;i<n;i++) G[i].clear();
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值