Following Orders
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 4436   Accepted: 1791

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
思路:拓扑排序;我们知道拓扑排序中有很多点是可以交换位置的,因为他们之间没有先后的约束,所以我们可以DFS求所有符合要求的拓扑排序输出
即可,注意要按字典序。
 1 #include<stdio.h>
2 #include<algorithm>
3 #include<iostream>
4 #include<string.h>
5 #include<queue>
6 #include<vector>
7 using namespace std;
8 char aa[100];
9 char bb[300];
10 int num1[100];
11 int num2[300];
12 int an[300];
13 int flag[300];
14 int dd[300];
15 vector<int>vec[300];
16 void top(int n,int ans,int k);
17 int main(void)
18 {
19 int i,j,k,p,q;
20 while(gets(aa)!=NULL)
21 { memset(an,0,sizeof(an));
22 memset(flag,0,sizeof(flag));
23 for(i=0;i<300;i++)
24 vec[i].clear();
25 gets(bb);int ans=0;
26 for(i=0;aa[i]!='\0';i++)
27 {
28 if(aa[i]!=' ')
29 {
30 num1[ans++]=aa[i]-'a';
31 }
32 }int cnt=0;sort(num1,num1+ans);
33 for(i=0;bb[i]!='\0';i++)
34 {
35 if(bb[i]!=' ')
36 {
37 num2[cnt++]=bb[i]-'a';
38 }
39 }
40 for(i=0;i<cnt-1;i+=2)
41 {
42 an[num2[i+1]]++;
43 vec[num2[i]].push_back(num2[i+1]);
44 }
45 for(i=0;i<ans;i++)
46 {
47 if(an[num1[i]]==0)
48 {
49 top(num1[i],ans,0);
50 }
51 }
52 printf("\n");
53 }return 0;
54 }
55 void top(int n,int ans,int k)
56 { dd[k]=n;int i,j;
57 if(k==ans-1)
58 {
59 for(i=0;i<ans;i++)
60 printf("%c",dd[i]+'a');
61 printf("\n");
62 return ;
63 }
64 flag[n]=1;
65 for(i=0;i<vec[n].size();i++)
66 {
67 int r=vec[n][i];
68 an[r]--;
69 }
70 for(i=0;i<ans;i++)
71 {
72 if(!flag[num1[i]]&&an[num1[i]]==0)
73 {
74 top(num1[i],ans,k+1);
75 }
76 }
77 flag[n]=0;
78 for(i=0;i<vec[n].size();i++)
79 {
80 int r=vec[n][i];
81 an[r]++;
82 }
83 }

 

Following Orders(poj1270)的更多相关文章

  1. POJ1270 Following Orders[拓扑排序所有方案 Kahn]

    Following Orders Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4885   Accepted: 1973 ...

  2. POJ1270 Following Orders (拓扑排序)

    Following Orders Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4254   Accepted: 1709 ...

  3. Cause: org.apache.ibatis.reflection.ReflectionException: Could not set property 'orderdetails' of 'class com.luchao.mybatis.first.po.Orders' with value 'Orderdetail [id=null, ordersId=3, itemsId=1, it

    从上面异常的解释来看是因为反射不能将Orders设置到orderdetails属性上,仔细检查了MyBatis的配置文件,发现: <collection property="order ...

  4. ACM/ICPC 之 拓扑排序+DFS(POJ1128(ZOJ1083)-POJ1270)

    两道经典的同类型拓扑排序+DFS问题,第二题较第一题简单,其中的难点在于字典序输出+建立单向无环图,另外理解题意是最难的难点,没有之一... POJ1128(ZOJ1083)-Frame Stacki ...

  5. poj 1731 Orders

    http://poj.org/problem?id=1731 Orders Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 9 ...

  6. 8.1:SportsStore:Orders and Administration

    本章,作者将通过收集和验证购物明细,来完成SportsStore应用,并在Deployd服务器上存储该订单.作者也构建了一个管理应用,允许认证用户查看订单,和管理产品分类. 1.准备实例项目 2.获取 ...

  7. 生成订单:三个表(Products,Orders,OrderItem)

    1.有三个表(Product上,Orders,OrderItem) 分别创建对应的三个实体类 OrderItem中有外键Order_id 参考Orders中的id :Product_id参考Produ ...

  8. POJ 1270 Following Orders

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

  9. How to Manage Amazon-Fulfilled Orders - Cancel an Amazon-Fulfilled Order

    You may request to cancel customer orders that have a status of "Pending" or "Unshipp ...

随机推荐

  1. Git分布式版本控制系统基础

    查看创建的账号 下来在该当前的⽬录下创建⽂件,并且进⾏提交 使⽤git log就可以看到最近提交的⽇志记录的信息 查看窗户的状态信息 某些时候我们可能需要回退到之前的版本,那么具体处理的步骤为: 1. ...

  2. Netty | 第1章 Java NIO 网络编程《Netty In Action》

    目录 前言 1. Java 网络编程 1.1 Javs NIO 基本介绍 1.2 缓冲区 Buffer 1.2 通道 Channel 1.3 选择器 Selector 1.4 NIO 非阻塞网络编程原 ...

  3. 数据集成工具—Sqoop

    数据集成/采集/同步工具 @ 目录 数据集成/采集/同步工具 Sqoop简介 Sqoop安装 1.上传并解压 2.修改文件夹名字 3.修改配置文件 4.修改环境变量 5.添加MySQL连接驱动 6.测 ...

  4. 开发安卓记账本-HelloAndroid的完成

    这个寒假要完成一个家庭记账本软件的开发,今天完成了Android Studio的安装与第一个安卓应用的运行(HelloAndroid) 下图是效果: 1.Android Studio的安装 可直接百度 ...

  5. 爬虫系列:连接网站与解析 HTML

    这篇文章是爬虫系列第三期,讲解使用 Python 连接到网站,并使用 BeautifulSoup 解析 HTML 页面. 在 Python 中我们使用 requests 库来访问目标网站,使用 Bea ...

  6. Linux启动初始化配置文件

    Linux启动初始化配置文件(1)/etc/profile 登录时,会执行. 全局(公有)配置,不管是哪个用户,登录时都会读取该文件. (2)/ect/bashrc Ubuntu没有此文件,与之对应的 ...

  7. 格式化代码(Eclipse 格式化代码块快捷键:Ctrl+Shift+F)

    1.格式化java代码 : ①Ctrl+Shift+F 但是我们会遇到按 Ctrl+Shift+F不起作用的时候?       Ctrl+Shift+F 在搜狗拼音里是简繁替换.一旦安装搜狗拼音这个快 ...

  8. 01_ubantu国内软件源配置

    查找自己版本对应的软件源 https://mirrors.tuna.tsinghua.edu.cn/help/ubuntu/ 以下为19.10版本清华大学的,个人100M的带宽,平均安装速度在600K ...

  9. 【C/C++】例题5-4 反片语/算法竞赛入门经典/C++与STL入门/映射:map

    本题是映射:map的例题. map:键值对. [题目] 输入一些单词,找出所有满足如下条件的单词:该单词不能通过字母重排,得到输入文本中的另外一个单词. 在判断是否满足条件时,字母不分大小写,但在输出 ...

  10. RocketMQ架构原理解析(三):消息索引

    一.概述 "索引"一种数据结构,帮助我们快速定位.查询数据 前文我们梳理了消息在Commit Log文件的存储过程,讨论了消息的落盘策略,然而仅仅通过Commit Log存储消息是 ...