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

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

——————————————————我是分割线——————————————————————————————
这题就是一个给定部分顺序,来确定整体顺序的拓扑排序。
但一般的拓扑排序只找出一种符合要求的序列,这题要求找出所有符合要求的序列,这就有点尴尬,
所以还得加上回溯算法。最后对求出的所有符合要求的序列进行排序输出就可以了。
(顺便练习一下sstream)
 /*
Problem:
OJ:
User: S.B.S.
Time:
Memory:
Length:
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<sstream>
#include<queue>
#include<cstdlib>
#include<iomanip>
#include<cassert>
#include<climits>
#include<functional>
#include<bitset>
#include<vector>
#include<list>
#include<map>
#define F(i,j,k) for(int i=j;i<=k;i++)
#define M(a,b) memset(a,b,sizeof(a))
#define FF(i,j,k) for(int i=j;i>=k;i--)
#define maxn 10001
#define inf 0x3f3f3f3f
#define maxm 1001
#define mod 998244353
//#define LOCAL
using namespace std;
int read(){
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n,m;
int a[maxn],d[maxn];
int pos[maxn],cnt[maxn][];
bool vis[maxn];
inline void dfs(int u)
{
if(u>n){
F(i,,n) cout<<(char)a[i];
cout<<endl;
return;
}
F(i,,n){
if(!vis[i]){
a[u]=d[i];
pos[a[u]]=u;
vis[i]=true;
bool flag=true;
for(int j=;j<=m&&flag;j++)
{
int aa=cnt[j][],bb=cnt[j][];
if(pos[aa]==||pos[bb]==||pos[aa]<pos[bb]);
else flag=false;
}
if(flag) dfs(u+);
pos[a[u]]=;
vis[i]=false;
}
}
}
int main()
{
// std::ios::sync_with_stdio(false);//cout<<setiosflags(ios::fixed)<<setprecision(1)<<y;
#ifdef LOCAL
freopen("data.in","r",stdin);
freopen("data.out","w",stdout);
#endif
string s;
istringstream ss;
char aa,bb,cc;
while(getline(cin,s))
{
M(vis,);M(pos,);
n=m=;ss.clear();
ss.str(s);
while(ss>>cc) d[++n]=cc;
sort(d+,d+n+);
getline(cin,s);
ss.clear();
ss.str(s);
while(ss>>aa>>bb){
cnt[++m][]=aa;
cnt[m][]=bb;
}
dfs();
cout<<endl;
}
return ;
}

poj 1270

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 (拓扑排序+回溯)

    Following Orders Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5473   Accepted: 2239 ...

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

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

  4. POJ 1270 Following Orders(拓扑排序)题解

    Description Order is an important concept in mathematics and in computer science. For example, Zorn' ...

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

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

  6. Day4 - H - Following Orders POJ - 1270

    Order is an important concept in mathematics and in computer science. For example, Zorn's Lemma stat ...

  7. poj 1270(toposort)

    http://poj.org/problem?id=1270 题意:给一个字符串,然后再给你一些规则,要你把所有的情况都按照字典序进行输出. 思路:很明显这肯定要用到拓扑排序,当然看到discuss里 ...

  8. poj 1270(dfs+拓扑排序)

    题目链接:http://poj.org/problem?id=1270 思路:就是一简单的dfs+拓扑排序,然后就是按字典序输出所有的情况. http://paste.ubuntu.com/59872 ...

  9. POJ 1731:Orders

    Orders Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 9940   Accepted: 6048 Descriptio ...

随机推荐

  1. 【51nod】1742 开心的小Q

    题解 我们由于莫比乌斯函数如果有平方数因子就是0,那么我们可以列出这样的式子 \(\sum_{i = 1}^{n} \sum_{d|i} (1 - |\mu(d)|)\) 然后枚举倍数 \(\sum_ ...

  2. 易普优APS-3C行业解决方案助力国家智能制造示范车间实现高效计划排程

    一.      项目背景 广东劲胜智能集团国家智能制造专项——移动终端金属加工智能制造新模式项目是2015年国家94家智能制1.造专项之一.本项目实施车间为金属CNC加工车间(下称“智能制造示范车间” ...

  3. 8-2 Party Games uva1610 (贪心)

    题意: 给出n个串(n为偶数): 要构造一个串,使n串中有一半小于等于它,另外一半大于它: 要求这个串长度尽量小,同时字典序小: 一开始我的优先级是放左   其实优先级是放左加一. 如 AAAA AA ...

  4. python3.6.5中pip3无法使用

    1.在python命令行窗口中: python3 -m ensurepip 创建出pip3.exe.2.再在python3.6的安装目录下的Scripts路径下命令行 pip3 install XXX ...

  5. python中的urlencode和urldecode

    python将字符串转化成urlencode ,或者将url编码字符串decode的方法: 方法1: urlencode:urllib中的quote方法 >>> from urlli ...

  6. 分分钟搞定Python之排序与列表

    排序时程序中用得比较多的方法了.在Python中,最简单的排序方法摸过与使用内置的sorted(list)这个函数了,该函数一一个列表作为参数返回一个新的列表,只不过是把旧列表中的元素排过序了.原列表 ...

  7. CORS跨域请求[简单请求与复杂请求]

    CORS即Cross Origin Resource Sharing(跨来源资源共享),通俗说就是我们所熟知的跨域请求.众所周知,在以前,跨域可以采用代理.JSONP等方式,而在Modern浏览器面前 ...

  8. Python进阶篇:文件系统的操作

    通过一个例子来熟悉文件的基本操作:创建文件,读取文件,修改文件,删除文件,重命名文件,判断文件是否存在 ''' 编写可供查询的员工信息表--学号 姓名 年龄 班级 1. 提供格式化查询接口 2. 允许 ...

  9. 解决springboot项目中@Value注解参数值为null的问题

    1.错误场景: springboot项目中在.properties文件(.yml)文件中配置了属性值,在Bean中使用@Value注解引入该属性,Bean的构造器中使用该属性进行初始化,此时有可能会出 ...

  10. SQL Server中执行正则表达式

    总体方案:写function,再执行update语句. 一.查询函数 -- ============================================= -- Author: <l ...