poj 1270 Following Orders

                                   Following Orders
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 5200   Accepted: 2119

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

题意:给你两个字符串,A串中的字符一定在竖直对应下来的B串的字符的前面,让你求出所有的合理的排列方法,并按照字典序排列。
思路:拓扑排序+爆搜即可。
错因:输出格式错误。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int n,num,sum,len;
char a[100],b[100],s[100];
int into[101],vis[101],v[110],tmp[101],map[110][110];
void dfs(int step,int pre){
    if(step==n){
        for(int i=0;i<n;i++)
            cout<<char(tmp[i]+'a');
        cout<<endl;
        return ;
    }
    for(int i=0;i<26;i++){
        if(vis[i]&&!v[i]&&!into[i]){
            v[i]=1;
            into[i]=-1;
            tmp[step]=i;
            for(int j=0;j<26;j++)
                if(map[i][j]&&into[j])    into[j]--;
            dfs(step+1,i);
            for(int j=0;j<26;j++)
                if(map[i][j])    into[j]++;
            into[i]=0;
            v[i]=0;
        }
    }
}
int main(){
    while(gets(a)){
        len=0;num=0;len=0;n=0;
        memset(v,0,sizeof(v));
        memset(tmp,0,sizeof(tmp));
        memset(vis,0,sizeof(vis));
        memset(map,0,sizeof(map));
        memset(into,0,sizeof(into));
        gets(b);
        for(int i=0;i<strlen(a);i++)
            if(a[i]!=' '){
                n++;
                vis[a[i]-'a']++;
            }
        for(int i=0;i<strlen(b);i++)
            if(b[i]!=' ')
                s[len++]=b[i];
        for(int i=1;i<len;i+=2){
            int x=s[i-1]-'a';
            int y=s[i]-'a';
            map[x][y]=1;
            into[y]++;
        }
        if(sum!=0)    cout<<endl;
        sum++;
        dfs(0,0);
    }
}
细雨斜风作晓寒。淡烟疏柳媚晴滩。入淮清洛渐漫漫。 雪沫乳花浮午盏,蓼茸蒿笋试春盘。人间有味是清欢。
原文地址:https://www.cnblogs.com/cangT-Tlan/p/7505097.html