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 思路:
简单的拓扑排序+dfs,通过入度判断,从1到26就有字典序,代码如下:
int in[], G[][], vis[], print[], num;
char ans[]; void init() {
num = ;
memset(in, , sizeof(in));
memset(G, , sizeof(G));
memset(vis, , sizeof(vis));
memset(print, , sizeof(print));
} void dfs(int u, int cnt) {
ans[cnt] = u - + 'a';
if(cnt == num) {
for(int i = ; i <= cnt; ++i)
cout << ans[i];
cout << "\n";
return;
}
// mark the point
print[u] = ;
for(int i = ; i <= ; ++i) {
if(vis[i] && G[u][i]) in[i]--;
}
for(int i = ; i <= ; ++i) {
if(vis[i] && !print[i] && !in[i]) {
dfs(i, cnt+);
}
// backtracing
}
for(int i = ; i <= ; ++i) {
if(vis[i] && G[u][i]) in[i]++;
}
print[u] = ;
} int main() {
ios::sync_with_stdio(false);
string t;
int t1, t2;
while(getline(cin, t)) {
init();
int siz = t.size();
for(int i = ; i < siz; i += ) {
vis[t[i] - 'a' + ] = ;
num++;
}
getline(cin, t);
siz = t.size();
for(int i = ; i + < siz; i += ) {
t1 = t[i] - 'a' + , t2 = t[i+] - 'a' + ;
G[t1][t2] = , in[t2]++;
}
for(int i = ; i <= ; ++i) {
if(vis[i] && !in[i]) {
dfs(i, );
}
}
cout << "\n";
} return ;
}

Day4 - H - Following Orders POJ - 1270的更多相关文章

  1. POJ 1270 Following Orders

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

  2. H - Buy Tickets POJ - 2828 逆序遍历 树状数组+二分

    H - Buy Tickets POJ - 2828 这个题目还是比较简单的,其实有思路,不过中途又断了,最后写了一发别的想法的T了. 然后脑子就有点糊涂,不应该啊,这个题目应该会写才对,这个和之前的 ...

  3. POJ 1270 Following Orders 拓扑排序

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

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

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

  5. poj 1270 Following Orders (拓扑排序+回溯)

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

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

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

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

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

  8. poj 1270(toposort)

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

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

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

随机推荐

  1. K8S-OVS使用Openvswitch为提供SDN功能支持单租户模式和多租户模式

    k8s-ovs ============================== 最近在寻求一些工作机会,如果有kubernetes相关研发招聘的朋友,欢迎随时联系我.我的个人简历可以通过百度网盘:htt ...

  2. git pull 之后怎么找回别覆盖掉的内容

    [半夜吓出冷汗,git这个原理还真得好好学学] 不小心把本地写的东西pull了下,然后,全部覆盖掉了,以为就这样没了. 后面想到有“时光穿梭机”,“历史回滚”,在各大群友的帮助下,终于找回了. git ...

  3. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 排版:设定文本左对齐

    <!DOCTYPE html> <html> <head> <title>菜鸟教程(runoob.com)</title> <meta ...

  4. 1009 Product of Polynomials (25分) 多项式乘法

    1009 Product of Polynomials (25分)   This time, you are supposed to find A×B where A and B are two po ...

  5. tensorflow中的图(02-1)

    由于tensorflow版本迭代较快且不同版本的接口会有差距,我这里使用的是1.14.0的版本 安装指定版本的方法:pip install tensorflow==1.14.0      如果你之前安 ...

  6. 5G时代,行业市场用户的公网与专网如何选择

    导读 今年,5G开启了真刀真枪的商用元年,尤其中国5G正式启动商用服务,5G规模商用进程再次大提速.除了面向消费者领域,5G更大的商业价值还是寄望于进入各个垂直行业,赋能千行百业数字化转型. 5G进入 ...

  7. Excel使用小技巧

    1.Excel随机设置单元格的内容为整数0或1: 在单元格中写公式:  =ROUND(RAND(),0) 2.设置某个单元格的值为1或0,根据其他3个单元格的值为0或1来确定: 在该单元格中写公式: ...

  8. Fiddler抓包(基本使用方法、web+app端抓包、篡改数据、模拟低速)

    1.HTTP代理原理图 http服务器代理:既是web服务器,又是web客户端 接口vs端口: 接口:包含地址和端口 端口:类似于USB接口 地址:127.0.0.1,端口默认:8888        ...

  9. Linux centosVMware 集群介绍、keepalived介绍、用keepalived配置高可用集群

    一.集群介绍 根据功能划分为两大类:高可用和负载均衡 高可用集群通常为两台服务器,一台工作,另外一台作为冗余,当提供服务的机器宕机,冗余将接替继续提供服务 实现高可用的开源软件有:heartbeat. ...

  10. ‘A’ = N’A’ , will not kill index seek in sqlserver version above 2014 anymore

    Thanks to The advisor show us that we can get different behavior when we use condition , ='20000' or ...