Day4 - H - Following Orders POJ - 1270
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
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
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的更多相关文章
- POJ 1270 Following Orders
Following Orders Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 4902 Accepted: 1982 ...
- H - Buy Tickets POJ - 2828 逆序遍历 树状数组+二分
H - Buy Tickets POJ - 2828 这个题目还是比较简单的,其实有思路,不过中途又断了,最后写了一发别的想法的T了. 然后脑子就有点糊涂,不应该啊,这个题目应该会写才对,这个和之前的 ...
- POJ 1270 Following Orders 拓扑排序
http://poj.org/problem?id=1270 题目大意: 给你一串序列,然后再给你他们部分的大小,要求你输出他们从小到大的所有排列. 如a b f g 然后 a<b ,b< ...
- POJ 1270 Following Orders (拓扑排序,dfs枚举)
题意:每组数据给出两行,第一行给出变量,第二行给出约束关系,每个约束包含两个变量x,y,表示x<y. 要求:当x<y时,x排在y前面.让你输出所有满足该约束的有序集. 思路:用拓扑排 ...
- poj 1270 Following Orders (拓扑排序+回溯)
Following Orders Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 5473 Accepted: 2239 ...
- POJ 1270 Following Orders(拓扑排序)题解
Description Order is an important concept in mathematics and in computer science. For example, Zorn' ...
- POJ 1270 Following Orders(拓扑排序)
题意: 给两行字符串,第一行为一组变量,第二行时一组约束(每个约束包含两个变量,x y 表示 x <y).输出满足约束的所有字符串序列. 思路:拓扑排序 + 深度优先搜索(DFS算法) 课本代码 ...
- poj 1270(toposort)
http://poj.org/problem?id=1270 题意:给一个字符串,然后再给你一些规则,要你把所有的情况都按照字典序进行输出. 思路:很明显这肯定要用到拓扑排序,当然看到discuss里 ...
- poj 1270(dfs+拓扑排序)
题目链接:http://poj.org/problem?id=1270 思路:就是一简单的dfs+拓扑排序,然后就是按字典序输出所有的情况. http://paste.ubuntu.com/59872 ...
随机推荐
- 了解 C++
C++的历史 C++由C语言发展演变而来,最初被称为"带类的C" 1983年正式取名为C++ 1998年11月被国籍标准化组织(ISO)批准为国际标准 2003年10月15日发布了 ...
- Hibernate笔记一
背景 jdbc的优缺点 A:直接操作底层,提供了简单,便捷的访问数据库方法,跨平台比较强,灵活,可以写很多赋值的SQL语句:是最底层的数据库操作,所以效率比较高,Sql语句可以自己选择写,采用效率最高 ...
- 从零构建以太坊(Ethereum)智能合约到项目实战——第22章 玩转truffle framework 、Web3.js 框架
P84 .1-玩转truffle framework.Web3.js 框架 内容介绍 truffle官方网站:https://truffleframework.com/ P85 .2-truffle ...
- 匈牙利命名法、Camel命名法与Pascal命名法
Camel命名法:即骆驼式命名法,首字母小写,采用该命名法的名称看起来就像骆驼的驼峰一样高低起伏.Camel命名法有两种形式: 1.混合使用大小写字母,例如runFast 2.单词之间加下划线,例如r ...
- 腾讯玄武实验室向(CNVD)提交了一个重大漏洞“BucketShock”
导读 11 月 21 日,在小米 IoT 安全峰会上,腾讯安全玄武实验室负责人于旸(花名:TK 教主)在演讲中透露,腾讯玄武实验室最近向国家信息安全漏洞共享平台(CNVD)提交了一个重大漏洞“Buck ...
- Sping IOC容器
Sping IOC容器 package servlet; import org.springframework.context.ApplicationContext; import org.sprin ...
- 图论初步<蒟蒻专属文章>
前言: 图论乃noip之重要知识点,但有点难理解 本人因此吃过不少亏 因为本人实在太弱,所以此篇乃正宗<蒟蒻专属文章> 正文:(本文仅介绍图论中的重点.难点,其余部分略将或不讲) 图 ...
- thinkPHP5.0中使用header跳转没作用
我在controller中的方法中这样写: header("Location:".$url); 但是一直没动静,不会跳转,最后还是官方文档解决了 https://www.kancl ...
- 【剑指Offer面试编程题】题目1387:斐波那契数列--九度OJ
题目描述: 大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项.斐波那契数列的定义如下: 输入: 输入可能包含多个测试样例,对于每个测试案例, 输入包括一个整数n(1< ...
- 第3节 storm高级应用:4、5、ack机制,以及其验证超时
4. 消息不丢失机制 4.1.ack是什么 ack 机制是storm整个技术体系中非常闪亮的一个创新点. 通过Ack机制,spout发送出去的每一条消息,都可以确定是被成功处理或失败处理, 从而可以 ...