Title: give you some lowercase letters, and some of the partial order pairs of lowercase letters, to find out all the permutations that satisfy the partial order relationship.
Analysis: Graph theory, search, topological sort. Ontology can be directly topological partial order, here directly search;
It is possible to reach the next point by judging the partial order relationship.
Description: Last week, Sunday went home to ╮(╯▽╰)╭.
#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]);p rintf ("\ 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); 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;cou NT + +;} Gets (temp); int number = 0,move = 0;while (Temp[move]) {if (Temp[move] >= ' A ' && temp[move] <= ' z ') temp[numb ER + +] = 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;} Find starting point set 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];d FS (Head[i], 1, CO UNT); Used[head[i]] = 0;}} return 0;}
UVa 124-following Orders