Title: give you a sequence of uppercase letters, which are given in a special dictionary order, to output the alphabetical sequence of known relationships.
Analysis: Graph theory, topological sequencing. Using DFS to sort the topology, log back the timestamp, reverse output can be.
Because it is a partial-order relationship, it has transitivity, so as long as you compare adjacent two strings.
Note: Finally, add a newline ╮(╯▽╰)╭.
#include <algorithm> #include <iostream> #include <cstdlib> #include <cstring> #include <cstdio> #include <cmath>using namespace Std;char list[10001][22];int maps[27][27 ];int used[27];int stack[27];int stack_size = 0;void dfs (int s) {Used[s] = 0;for (int i = 0; i <) + + i) if (Used[i] && Maps[s][i]) {DFS (i);} Stack[stack_size + +] = s+ ' A ';} int main () {int count = 0;while (scanf ("%s", List[count]) && list[count][0]! = ' # ') + count;memset (maps, 0, sizeof (m APS)); memset (used, 0, sizeof (used)); used[list[0][0]-' a '] = 1;//has only one set for (int i = 1; i < count; + + i) {for (int p = 0, q = 0; LIST[I-1][P] && list[i][q]; + + P, + + q) if (list[i-1][p]! = List[i][q]) {maps[list[i-1][p]-' a '][list[i][q]-' a '] = 1;used[list[i-1][p]-' a '] = Used[list I [q]-' A '] = 1;break;}} for (int i = 0; i <, + + i) if (Used[i]) DFS (i), while (Stack_size-) printf ("%c", Stack[stack_size]);p rintf ("\ n"); return 0;}
UVa 200-rare Order