Following Orders(poj1270)

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 }

 
时间: 2024-08-04 19:20:33

Following Orders(poj1270)的相关文章

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

Following Orders Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4885   Accepted: 1973 Description Order is an important concept in mathematics and in computer science. For example, Zorn's Lemma states: ``a partially ordered set in which

POJ1270 Following Orders (拓扑排序)

Following Orders Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4254   Accepted: 1709 Description Order is an important concept in mathematics and in computer science. For example, Zorn's Lemma states: ``a partially ordered set in which

POJ1270 Following Orders(拓扑排序)

Following Orders Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4059   Accepted: 1623 Description Order is an important concept in mathematics and in computer science. For example, Zorn's Lemma states: ``a partially ordered set in which

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

1.有三个表(Product上,Orders,OrderItem) 分别创建对应的三个实体类 OrderItem中有外键Order_id 参考Orders中的id :Product_id参考Product中的id:Orders中有外键User_id参考User中的id sql 代码: CREATE DATABASE day2017_04_05; USE day2017_04_05; CREATE TABLE `user` ( `id` INT(11) AUTO_INCREMENT, `usern

POJ - 1731 Orders

题意:排序后字符串全排列 思路:好久没水一题了 #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int MAXN = 220; char str[MAXN]; int main() { while (scanf("%s", str) != EOF) { int n = strle

8.1:SportsStore:Orders and Administration

本章,作者将通过收集和验证购物明细,来完成SportsStore应用,并在Deployd服务器上存储该订单.作者也构建了一个管理应用,允许认证用户查看订单,和管理产品分类. 1.准备实例项目 2.获取产品明细 在给用户显示购物车中的产品汇总后,作者将购物明细用于订单.这需要使用AngularJS的表单特性,你会在大多数web应用中需要.作者已经创建Views/placeOrder.html文件,捕获用户的购物明细.作者将介绍一些与表单相关度的特性,来避免重复大量相似的代码.作者首先添加一组数据属

poj 1270 Following Orders(拓扑排序+dfs)

大致题意:每个样例包含两行,第一行输入n个字符,可能是无序的.第二行输入成对的a b,代表a要在b前面.输出所有的符合这样的序列. 思路:很明显的拓扑排序.要输出所有的序列,那么就从入度为0的点进行dfs,每次选择一个入度为0的点,加入输出序列并把与它相邻的点的入度减一.dfs结束后要把状态再改回来. #include <stdio.h> #include <algorithm> #include <set> #include <map> #include

Bob&#39;s stroe orders put in file and view

$DOCUMENT_ROOT = $_SERVER['DOCUMENT_ROOT']; $fp = fopen("$DOCUMENT_ROOT/../orders/orders.txt",'w'); $DOCUMENT_ROOT 是脚本所在文件根目录 就是www文件夹   ".."是表示根目录的父目录 即wamp文件夹  是为了后面方便读取 <?php // creat short variable $tireqty = $_POST['tireqty'];

poj1270拓扑排序

题意:给定一些大小关系,把关系从大到小排序,如果有多种相同关系就按字典序排序.例如 x < y && x < z 则满足关系的有xyz 和 xzy . 思路:我们可以把每组大小关系建一条有向边,这样就形成了一个有向图,再对有向图进行拓扑,得到的结果即满足.但是拓扑出来的是一组可行解,而题意是要输出所有可行解,刚好可以想到递归求拓扑时可以回溯:那样就可以把所有的可行解输出.不过要把所有的变量排序. 代码: #pragma warning (disable: 4786) #incl