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. 插头DP学习笔记——从入门到……????

    我们今天来学习插头DP??? BZOJ 2595:[Wc2008]游览计划 Input 第一行有两个整数,N和 M,描述方块的数目. 接下来 N行, 每行有 M 个非负整数, 如果该整数为 0, 则该 ...

  2. lr参数化取值与连接数据库

    TXT文本,EXCEL表格以及数据库中的表都可以作为参数的数据集载体,LR都是支持的. 特别提醒: 1.在形成数据池之后,数据库中的数据变化不会影响数据池中的数据. 2.数据文件一定要以一个空行结束, ...

  3. LoadRunner 一参多用

    LoadRunner参数化后的值在脚本中多处位置引用(LoadRunner 一参多用)   LoadRunner的参数化给了我们很多便利,但是当一个脚本中同一个值出现多处,并且值都是一致的.这个时候, ...

  4. 8-1 binpacking uva1149(贪心)

    题意:给定N个物品的重量Li  背包的容量M 同时要求每个背包最多装两个物品 求至少要多少个背包才能装下所有物品 简单贪心  注意输出: #include<bits/stdc++.h> u ...

  5. CODEVS 4655 序列终结者-splay(区间更新、区间翻转、区间最值)

    4655 序列终结者  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 大师 Master 题解       题目描述 Description 网上有许多题,就是给定一个序列,要 ...

  6. 使用GNU工具链进行嵌入式裸机开发

    Embedded-Programming-with-the-GNU-Toolchain Vijay Kumar B. vijaykumar@bravegnu.org 翻译整理:thammer gith ...

  7. CSS基础-DAY2

    CSS属性操作-文本 文本颜色 <head> <style> p{ /*color:#8B5742 ;色码表*/ color: RGBA(255,0,0,0.5); /*调色, ...

  8. Keystone几种token生成的方式分析

    从Keystone的配置文件中,我们可见,Token的提供者目前支持四种. Token Provider:UUID, PKI, PKIZ, or Fernet 结合源码及官方文档,我们用一个表格来阐述 ...

  9. python list的应用

    先看下面的操作 In [2]: lis = [(1,2),(3,4),(5,6)] In [3]: for a,b in lis: ...: if a == 1: ...: print (" ...

  10. JZYZOJ 2002 [cf] 石江豪pk李震 博弈论 sg函数

    http://172.20.6.3/Problem_Show.asp?id=2002 https://blog.csdn.net/qq_24451605/article/details/5015497 ...