UVa 124 - Following Orders

题目:给你一些小写字母,以及一些小写字母的偏序对,求出所有满足偏序关系的排列。

分析:图论,搜索,拓扑排序。本体可直接拓扑偏序,这里直接搜索;

利用偏序关系的判断能否到达下一个点即可。

说明:最后一周了,周日就回家了╮(╯▽╰)╭。

#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>

using namespace std;

char buf[55],temp[5000];
int  map[55][55],space[256],used[55],head[55],save[55];

void dfs(int s, int d, int n)
{
	if (d == n) {
		for (int i = 0 ; i < n ; ++ i)
			printf("%c",buf[save[i]]);
		printf("\n");
		return;
	}
	for (int i = 0 ; i < n ; ++ i) {
		int flag = 1;
		for (int j = 0 ; j < d ; ++ j)
			if (map[i][save[j]]) {
				flag = 0;break;
			}
		if (flag && !used[i]) {
			used[i] = 1;
			save[d] = i;
			dfs(i, d+1, n);
			used[i] = 0;
		}
	}
}

int main()
{
	int t = 0;
	while (gets(buf)) {
		if (t ++) printf("\n");

		memset(space, 0, sizeof(space));
		for (int i = 0 ; buf[i] ; ++ i)
			space[buf[i]] = 1;
		int count = 0;
		for (int i = 'a' ; i <= 'z' ; ++ i)
			if (space[i]) {
				space[buf[count] = i] = count;
				count ++;
			}

		gets(temp);
		int number = 0,move = 0;
		while (temp[move]) {
			if (temp[move] >= 'a' && temp[move] <= 'z')
				temp[number ++] = temp[move];
			move ++;
		}
		memset(map, 0, sizeof(map));
		for (int i = 0 ; i < number ; i += 2) {
			map[space[temp[i]]][space[temp[i+1]]] = 1;
			map[i][i] = 1;
		}

		//查找起始点集合
		int size = 0;
		for (int i = 0 ; i < count ; ++ i) {
			int flag = 1;
			for (int j = 0 ; j < count ; ++ j) {
				if (i == j) continue;
				if (map[j][i]) {
					flag = 0;break;
				}
			}
			if (flag) head[size ++] = i;
		}

		memset(used, 0, sizeof(used));
		for (int i = 0 ; i < size ; ++ i) {
			used[head[i]] = 1;
			save[0] = head[i];
			dfs(head[i], 1, count);
			used[head[i]] = 0;
		}
	}
    return 0;
}
时间: 2024-08-01 06:01:48

UVa 124 - Following Orders的相关文章

UVA 124 &amp; POJ 1270 Following Orders(拓扑排序)

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=60 http://poj.org/problem?id=1270 Following Orders Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 3806   Accepted: 1507 Description Or

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

POJ 1270 Following Orders

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

poj1270Following Orders(拓扑排序+dfs回溯)

题目链接: 啊哈哈,点我点我 题意是: 第一列给出所有的字母数,第二列给出一些先后顺序.然后按字典序最小的方式输出所有的可能性... 思路: 总体来说是拓扑排序,但是又很多细节要考虑,首先要按字典序最小的方式输出,所以自然输入后要对这些字母进行排列,然后就是输入了,用scanf不能读空格,所以怎么建图呢??设置一个变量判断读入的先后顺序,那么建图完毕后,就拓扑排序了,那么多种方式自然就是dfs回溯了..那么这个问题就得到了解决.. 题目: Following Orders Time Limit:

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

jpa

学习尚硅谷jpa笔记: 所依赖的jar包: 首先在META-INF下创建配置文件,persistence.xml 1 <?xml version="1.0" encoding="UTF-8"?> 2 <persistence version="2.0" 3 xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.

[题解]UVa 12661 Funny Car Racing - spfa

很简单的一道最短路问题.分情况处理赛道的打开和关闭. Code 1 /** 2 * UVa 3 * Problem#12661 4 * Accepted 5 * Time:50ms 6 */ 7 #include<iostream> 8 #include<fstream> 9 #include<sstream> 10 #include<cstdio> 11 #include<cstdlib> 12 #include<cstring>

[题解]UVa 11082 Matrix Decompressing

开始眨眼一看怎么也不像是网络流的一道题,再怎么看也觉得像是搜索.不过虽然这道题数据范围很小,但也不至于搜索也是可以随随便便就可以过的.(不过这道题应该是special judge,因为一题可以多解而且题目中然而并没有什么要求,所以说可以考虑思考一下这道题有木有什么"套路"之类的通法) 比如说有这么一组数据 原矩阵 1 2 3 4 7 8 9 5 6 输入 3 3 6 25 45 14 28 45 然后将每一行的和写在每一列对应的行上(很明显有问题) 6 0 0 19 0 0 20 0

[uva] 10099 - The Tourist Guide

10099 - The Tourist Guide 题目页:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1040 Mr.G.自己也算一个人……… 反映到代码里是127行 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 3