Problem description:
There are n strings with the length of m + 1. If the last M character of a string matches the first M character of a string, the two strings can be connected. Ask how long the N strings can be connected together. If a loop occurs, an error is returned.
Analysis:
Think of each string as the vertex of a graph. The two strings can be connected to form a directed edge. It is equivalent to determining whether a directed graph has a ring and finding the longest path of the directed graph.
You can use the depth-first traversal algorithm of the graph to solve the problem. The graph is represented by an adjacent table.
1. Determine whether the directed graph has a ring and the longest path.
# Define vertex_num7 # define NOT_VISITED-1 // The vertex is not accessed # define visited0 // The vertex has been accessed, however, its adjacent contacts are not fully accessed # define finished1 // The endpoint list for accessing the vertex # define IS_CIRCLE-1int maxlen =-1; int getmaxleningraph (INT adj [] [vertex_num]); // return-1 indicates that the directed graph contains the loop int dfs_visti (INT State [], int adj [] [vertex_num], int I, int * Len ); // advanced traversal of int getmaxleningraph (INT adj [] [vertex_num]) {int State [vertex_num]; for (INT I = 0; I <vertex_num; I ++) state [I] = Not_visited; int templen; For (INT I = 0; I <vertex_num; I ++) {templen = 0; If (State [I] = not_visited) {If (dfs_visti (State, adj, I, & templen) = is_circle) return is_circle ;}return maxlen ;}// State [I] stores the status of vertex I, all vertex int dfs_visti (INT State [], int adj [] [vertex_num], int I, int * Len) adjacent to vertex I are stored in the array) {State [I] = visited; For (Int J = 1; adj [I] [J]! =-1; j ++) {If (State [adj [I] [J] = not_visited) {(* Len) ++; If (dfs_visti (state, adj, adj [I] [J], Len) = is_circle) return is_circle;} else if (State [adj [I] [J] = visited) // This indicates the existence of the ring. Can it stand the test? {* Len = is_circle; return is_circle;} State [I] = finished; If (* Len> maxlen) maxlen = * Len; * Len = 0; return 0 ;}
Test code:
Int main () {// the data in the graph's adjacent table.-1 indicates that the end of int adj [vertex_num] [vertex_num] = {0, 1, 3,-1 }, {1, 2, 3, 4,-1}, {2, 3, 4, 5,-1}, {3, 4,-1}, {4, 5, -1}, {5, 6,-1}, {6,-1} // change to 6, 0,-1 indicates that the ring exists }; int Len = getmaxleningraph (adj); printf ("the length of graphic is: % d \ n", Len); System ("pause"); Return 0 ;}
2. Solve the longest string that can be connected (first create a graph and then use the function provided above to solve it)
The code is implemented as follows:
# Include <iostream> # include <string> using namespace STD; # define vertex_num8 # define NOT_VISITED-1 // The vertex is not accessed # define visited0 // The vertex has been accessed, however, its adjacent contacts are not fully accessed # define finished1 // The endpoint list for accessing the vertex # define IS_CIRCLE-1int maxlen =-1; int getmaxleningraph (INT adj [] [vertex_num]); // return-1 indicates that the directed graph contains the loop int dfs_visti (INT State [], int adj [] [vertex_num], int I, int * Len ); // The depth first traverses void build_graph (string STR [], int graph [] [vertex_num] ); Int main () {string STR [] = {"ABC", "BCF", "cfg", "FGA", "Gam", "BCG ", "CGA", "Gai",}; // Its Adjacent table. If the four nodes are changed to "Gab ", the following error occurs: // 0-1-5 // 1-2 // 2-3 // 3-4-7 // 4 // 5-6 // 6-4 -7 // 7int graph [vertex_num] [vertex_num]; // build_graph (STR, graph); int Len = getmaxleningraph (graph); printf ("the longest path of the directed graph is (-1 indicates that a ring exists ): % d \ n ", Len); If (Len! =-1) printf ("the longest string that can be connected is % d \ n", STR [0]. length () + Len); System ("pause"); Return 0;} void build_graph (string STR [], int gra [] [vertex_num]) {for (INT I = 0; I <vertex_num; I ++) for (Int J = 0; j <vertex_num; j ++) {If (j = 0) gra [I] [J] = I; elsegra [I] [J] =-1;} int index; For (INT I = 0; I <vertex_num; I ++) {Index = 1; string suffix = STR [I]. substr (1); For (Int J = 0; j <vertex_num; j ++) {If (STR [J]. find (suffix) = 0) gra [I] [index ++] = J ;}}}
It is inevitable that a problem occurs. please correct me if you find any mistake!
Note: The stack overflow problem will be prompted during running. Modify the stack reserve size and stack commit size in property> Configuration Properties> linker> system ~