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

题意:第一行表示给出的字母,第二行每两个一组(x,y)表示x<y,要你把这行字母的顺序打出来,不唯一就按字典序打

思路:先把各自关系放在一个表里,然后按顺序dfs。因为每次都会判断后面的元素会不会大于前面的,所以矛盾的就不会输出。

代码:

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<queue>
#include<cmath>
#include<string>
#include<stack>
#include<set>
#include<map>
#include<vector>
#include<iostream>
#include<algorithm>
#include<sstream>
#define ll long long
const int N=35;
const int INF=1e9;
using namespace std;
int is[N],vis[N],mp[N][N],ans[N],size; int can(int x,int cnt){
for(int i=0;i<cnt;i++){
if(mp[ans[i]][x]) return 0; //i大于x,x不能放i后面
}
return 1;
} void dfs(int cnt){
if(cnt==size){
for(int i=0;i<size;i++){
printf("%c",ans[i]+'a');
}
cout<<endl;
return;
}
for(int i=0;i<26;i++){
if(is[i] && !vis[i] && can(i,cnt)){
vis[i]=1;
ans[cnt]=i;
dfs(cnt+1);
vis[i]=0;
}
} }
int main(){
char s[1000];
while(gets(s)){
memset(is,0,sizeof(is));
memset(vis,0,sizeof(vis));
memset(mp,0,sizeof(mp));
int len=strlen(s);
size=0;
for(int i=0;i<len;i++){
if(s[i]!=' '){
is[s[i]-'a']=1;
size++;
}
}
gets(s);
len=strlen(s);
for(int i=0;i<len;i++){
if(s[i]!=' '){
int a=s[i++]-'a';
while(s[i]==' ') i++;
int b=s[i]-'a';
mp[b][a]=1; //a<b,表示b大于a存在
}
}
dfs(0);
cout<<endl;
}
return 0;
}

POJ 1270 Following Orders(拓扑排序)题解的更多相关文章

  1. POJ 1270 Following Orders 拓扑排序

    http://poj.org/problem?id=1270 题目大意: 给你一串序列,然后再给你他们部分的大小,要求你输出他们从小到大的所有排列. 如a b f g 然后 a<b ,b< ...

  2. POJ 1270 Following Orders (拓扑排序,dfs枚举)

    题意:每组数据给出两行,第一行给出变量,第二行给出约束关系,每个约束包含两个变量x,y,表示x<y.    要求:当x<y时,x排在y前面.让你输出所有满足该约束的有序集. 思路:用拓扑排 ...

  3. POJ 1270 Following Orders(拓扑排序)

    题意: 给两行字符串,第一行为一组变量,第二行时一组约束(每个约束包含两个变量,x y 表示 x <y).输出满足约束的所有字符串序列. 思路:拓扑排序 + 深度优先搜索(DFS算法) 课本代码 ...

  4. POJ 1270 Following Orders

    Following Orders Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4902   Accepted: 1982 ...

  5. POJ 2367 (裸拓扑排序)

    http://poj.org/problem?id=2367 题意:给你n个数,从第一个数到第n个数,每一行的数字代表排在这个行数的后面的数字,直到0. 这是一个特别裸的拓扑排序的一个题目,拓扑排序我 ...

  6. poj 3687 Labeling Balls(拓扑排序)

    题目:http://poj.org/problem?id=3687题意:n个重量为1~n的球,给定一些编号间的重量比较关系,现在给每个球编号,在符合条件的前提下使得编号小的球重量小.(先保证1号球最轻 ...

  7. [ACM] POJ 3687 Labeling Balls (拓扑排序,反向生成端)

    Labeling Balls Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10161   Accepted: 2810 D ...

  8. poj 2762(强连通分量+拓扑排序)

    题目链接:http://poj.org/problem?id=2762 题意:给出一个有向图,判断任意的两个顶点(u,v)能否从u到达v,或v到达u,即单连通,输出Yes或No. 分析:对于同一个强连 ...

  9. POJ 2585.Window Pains 拓扑排序

    Window Pains Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1888   Accepted: 944 Descr ...

  10. POJ 1128 Frame Stacking (拓扑排序)

    题目链接 Description Consider the following 5 picture frames placed on an 9 x 8 array. ........ ........ ...

随机推荐

  1. mysql python pymysql模块 获取插入的最后一条数据的自增ID lastrowid()方法

    调用游标下的lastrowid 可以获取插入之前的表里id字段存放到哪个自增id cursor.lastrowid mysql> select * from userinfo; +----+-- ...

  2. 远程开关机神器Wake On LAN,免费有中文版

    https://wol.aquilatech.com/ Wake On Lan 又名 aquilaWOL,这是一款免费且开源的图形界面 WOL 软件,有繁体中文界面,可以管理多台电脑和网络设备,支持批 ...

  3. 【剑指offer】包含min函数的栈

    一.题目: 定义栈的数据结构,请在该类型中实现一个能够得到栈中所含最小元素的min函数. 二.思路: 无,Z(zhi)Z(zhang)式操作. 三.代码:    

  4. ffmpeg综合应用示例(三)——安卓手机摄像头编码

    本文的示例将实现:读取安卓手机摄像头数据并使用H.264编码格式实时编码保存为flv文件.示例包含了 1.编译适用于安卓平台的ffmpeg库 2.在java中通过JNI使用ffmpeg 3.读取安卓摄 ...

  5. vertx 从Tcp服务端和客户端开始翻译

    写TCP 服务器和客户端 vert.x能够使你很容易写出非阻塞的TCP客户端和服务器 创建一个TCP服务 最简单的创建TCP服务的方法是使用默认的配置:如下 NetServer server = ve ...

  6. js 的each()方法遍历对象和数组

    <script src="../lib/jquery-1.8.3.min.js" ></script> <script type="text ...

  7. 实习培训——Servlet(5)

    实习培训——Servlet(5) 1  Servlet 简介 Servlet 是什么? Java Servlet 是运行在 Web 服务器或应用服务器上的程序,它是作为来自 Web 浏览器或其他 HT ...

  8. iOS UI基础-1.0加法计算器

    1.打开Xcode,新建一个项目 2.Single View Application是最适合初学者的模板 3.填写该应用相关信息 4.搭建UI界面 项目创建完毕后,自动帮我们做了很多配置,也自动生成了 ...

  9. Python线程,进程,携程,I/O同步,异步

    只有本人能看懂的-Python线程,进程,携程,I/O同步,异步 举个栗子: 我想get三个url,先用普通的for循环 import requests from multiprocessing im ...

  10. c#null值加法运算

    加号都是一个含义啊,操作数不同,加号重载的方法就不一样,当加号的左边或右边含有字符串的时候,总是返回一个不为空的字符串.当加号左右两边都是数值的时候,就会对其进行数学运算,null+任何数都为null ...