Test instructions: Given some strings, the default is that there is some sort of partial ordering between all characters (if the input is satisfied, the order is good), such as {"Free", "GBK", "Atoi"}, the characters that precede each string are always less than or equal to the following characters.
The idea of solving the problem is to think of any two adjacent characters in each string as the partial order relation of <=, and establish a graph to do a topological sort of this graph.
Two common algorithms for topological sequencing: 1. The use of the degree of 0, using the queue to process; 2. Use DFS to reverse the output of nodes in a stack order.
public classsolution{private Static BooleanMygraph[][]; private static BooleanValid[]; private static voidBuildmap (String sortedsequence[]) {mygraph = new boolean[128][128]; valid = new boolean[128]; Initial my graph for (int i = 0; i < sortedsequence.length; + +)i) {Valid[sortedsequence[i].charat (0)] = True; for (int j = 1; J < Sortedsequence[i].length (); + +j) {Valid[sortedsequence[i].charat (j)] = True; if (Sortedsequence[i].charat (j-1)! =Sortedsequence[i]. CharAt (j)) {Mygraph[sortedsequence[i].charat (j-1)][sortedsequence[i]. CharAt (j)] = True; }}}}//Solve by using a queue public staticString Solve_0 (String sortedsequence[]) {buildmap (sortedsequence);//topological sort java.util.queue<integer> Auxqueue = new Java.util.linkedlist<integer>(); int indegrees[] = new int[128]; int chars = 0; for (int i = 1; i < 128; + +i) {chars + = Valid[i]? 1:0; } for (int i = 1; i < 128; + +i) {if(Valid[i]) {for (int j = 1; j < 128; + +)j) {if (i! = J &&Mygraph[i][j]) {+ +INDEGREES[J]; }}}} for (int i = 1; i < 128; + +)i) {if (Valid[i] && indegrees[i] = = 0) {Auxqueue.add (i);}} String ret = ""; while (!Auxqueue.isempty ()) {int cur =Auxqueue.remove (); RET + = (char) cur; for (int i = 1; i < 128; + +i) {if (cur! = i &&Mygraph[cur][i]) {if (--indegrees[i] = = 0) {Auxqueue.add (i);}} }} return Ret.length () = = chars? Ret:null; } Private enumColor {white, GRAY, BLACK, disappeared}; Solve by DFS public staticString Solve_1 (String sortedsequence[]) {java.util.stack<integer> auxstack = new java.util.stack<integer>(); Color charcolor[] = new color[128]; for (int i = 1; i < 128; + +i) {Charcolor[i] =Color. White; } String ans = ""; for (int k = 1; k < 128; + +k) {if (Valid[k] && charcolor[k] = =color. White) {Charcolor[k] = color. GRAY; Auxstack.push (k); while (! Auxstack.isempty ()) {int cur = Auxstack.peek (); if (charcolor[cur] = = color. GRAY) {Charcolor[cur] = color. BLACK; for (int i = 1; i <; + +i) {if (Valid[i] && cur! = i && mygraph[cur][i]) {if (Charcolor[i] = = Color. BLACK)//back edge {return null;} if (charcolor[i] = = color. White) {Charcolor[i] = color. GRAY; Auxstack.push (i); }}}} else {ans = (char) cur + ans; Auxstack.pop (); charcolor[cur] = color. disappeared; }}}}} return ans, public static void main (string[] args) {string[] A = {"FT", "FCP", "AAC", "act", "ACD", "ATP", "Tbk", "TfD" }; System.out.println (Solve_0 (A)); System.out.println (Solve_1 (A)); }}
The
uses topological ordering to find the partial order relationship in some strings.