题目如下:
Following Orders
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 4317 | Accepted: 1729 |
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.
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.
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.
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
Source
代码各变量的意义如下:
variables[],存放每个待排序的字母;
constraints[],存放约束,通过这个数组来构造m_index[][]矩阵。这里由于是读入string类型,所以将约束数据从string变量中读取到constraints中,然后再进行解析,生成m_index;
selected[],标记搜索过的字母,它初始化时,都是0,搜索后将它标记为1;
result[],存放输出的结果,代码在搜索到最后一层时,打印出result;
m_hash,它是一个map类型的容器变量,用它来实现直接通过给定一个字母就搜索到它在variables数组的下标;
简单的思路整理如下:
注意点:1.每组数据输出要空一行;2.在每组测试数据进行前,一定要清空上一轮所存储的数据,我就是在这里一直WA;3.在回溯的时候进行剪枝,一般不会超时。4.输出的答案是要求按字典顺序排序的,所以在构造好variables数组时,就可对它进行排序。
代码如下:
#include <iostream>
#include <string>
#include <string.h>
#include <stdio.h>
#include <map>
#include <algorithm>
using namespace std;
string input;
size_t test_len;
char variables[21]; // 输入的字符组成的数组
char result[21];
char selected[21];
char constraints[101]; // 约束
int m_index[21][21]; // 存储比较大小的结构
map<char, int> m_hash;
map<char, int>::iterator m_hash_it;
bool flag = true;
// 分割字符串, 并且建立m_hash
void split_string(string input, char* v, map<char, int> *m_hash) {
size_t j = 0;
for (size_t i = 0; i != input.size(); ++i) {
if (input[i] != ' ')
v[j++] = input[i];
}
v[j] = '\0';
sort(v, v + strlen(v)); // 排序
for (size_t i = 0; i != strlen(v); ++i)
m_hash -> insert(map<char, int>::value_type(v[i], i));
}
// 分割字符串,生成约束变量数组
void gen_constraints(string input, char* v){
int j = 0;
for (size_t i = 0; i != input.size(); ++i) {
if (input[i] != ' ') {
v[j++] = input[i];
}
}
v[j] = '\0';
}
// 将m_index变量初始化为0
void init_m_index(int m_index[][21], int n) {
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
m_index[i][j] = 0;
}
// 比较a是否小于b
int is_legal(int a, int b) {
if (m_index[a][b] == 1)
return 0;
else
return 1;
}
// 判断result之前的点是否都小于a
int is_result_legal(int a, int level) {
for (int i = 0; i <= level; ++i){
if (!is_legal(m_hash.find(result[i])->second, a))
return 0;
}
return 1;
}
// 利用回溯法来生成所有遍历结果
void gen_result(int root, int level) {
selected[root] = 1;
result[level] = variables[root];
if (!is_result_legal(root, level)) {
selected[root] = 0;
flag = false;
return;
}
if (level == test_len - 1){
result[test_len] = '\0';
cout << result << endl;
selected[root] = 0;
return;
}
for (int i = 0; i < test_len; ++i) {
if (selected[i] == 0){
gen_result(i, level + 1);
if (!flag) { // 利用flag进行剪枝
flag = true;
selected[root] = 0;
return;
}
}
}
selected[root] = 0;
}
int main(){
while (getline(cin, input)) {
// 读入变量
m_hash.clear(); // 因为少了这行代码,贡献了特别多WA。。。
split_string(input, variables, &m_hash);
init_m_index(m_index, 21);
test_len = strlen(variables);
// 读入约束
getline(cin, input);
gen_constraints(input, constraints);
// 生成约束的结构
for (int i = 0; i < strlen(constraints); i += 2) {
m_index[m_hash.find(constraints[i + 1])->second][m_hash.find(constraints[i])->second] = 1;
}
// 输出结果
for (int i = 0; i < test_len; ++i) {
memset(selected, 0, sizeof selected);
memset(result, 0, sizeof result);
gen_result(i, 0);
}
cout << endl;
}
}