Word Stitching
-
- Describe
-
Give you some words, and ask if you can string them together in a string.
The end of the previous word should be the same as the letter of the next word.
Such as
Aloha
Dog
Arachnid
Gopher
Tiger
Rat
Can be stitched into:Aloha.arachnid.dog.gopher.rat.tiger
-
- Input
-
The
- first line is an integer N (0<n<20) that represents the number of groups of test data
The first line of each set of test data is an integer m, indicating that the group of test data has M (2<m<1000) a different word, followed by M-line, each line is a word of not more than 30, the words are all composed of lowercase letters.
-
- Output
-
- If there is a tiling scheme, please output the scheme with the smallest dictionary order in all stitching schemes. (output a period of two words ".")
If there is no tiling scheme, the output
***
-
- Sample input
-
-
26alohaarachniddoggopherrattiger3oakmapleelm
-
- Sample output
-
-
aloha.arachnid.dog.gopher.rat.tiger***
-
-
the graph G is a Euler circuit:
when and only if the base diagram of G is connected, and the penetration of all vertices is equal to the degree.
there is a direction graph G for the European pull Road:
when and only if the base diagram of G is connected, and there is only one vertex you have a 1 greater than the degree of penetration,
only one vertex v is 1 less than the degree of penetration, and all other vertices are in degrees equal to the degrees.
Analysis: This problem should be the first letter and the end of the letter out, the first letter out of the degree of +1, the end of the letter into the +1, so that each letter can be based on the degree and the degree of determination of the beginning and end. In particular, if it is the Euler circuit, any point can be used as a starting point, when we follow the test instructions to select the dictionary in the order of the front.
#include <stdio.h> #include <string.h> #include <stdlib.h>char str[1000][31];//storage string bool used[1000] ; DFS when judging whether the word used int in[26]; in degrees int out[26]; The s[1000];//int is used in judge to hold the size of each string sizeint stack[1000]; DFS store result int n;int compare (const void *a, const void *b)//String Sort {char *p1 = (char *) A;char *P2 = (char *) B;return strcmp (p 1, p2);} int Judge ()//detect Euler {int I;int last = -1;int first = -1;for (i = 0; i <; ++i) {if (In[i]! = Out[i]) {//out is the number of occurrences at the beginning of the string, in is the end of the string Current count if (Out[i]-in[i] = = 1 && First = = 1) First = I;else if (out[i]-in[i] = = 1 && last = = 1) last = i; elsereturn-1; }}if (First >-1 && last >-1)//Oraton Road return First;else if (first = =-1 && last = = 1)//Euler loop {fo R (i = 0; i < ++i) if (in[i]! = 0) return i;} elsereturn-1;} BOOL DFS (char First, int Index)//There may be more than one connected region {if (Index = = N)//Added this judgment. You don't have to check the set. Return true;int I;int b,e,m;b = 0;e = n-1; while (b <= e)//dichotomy find this first character, sorted well, certainly fast ah. Speed is definitely twice times faster than others {m = (b + e)/2;if (str[m][0] = =First) Break;else if (Str[m][0] > First) e = M-1;elseb = m + 1;} if (b > E) return false;//finds the first occurrence of this character string while (str[m][0] = = primary && m >= 0)--m;for (i = m + 1; str[i][0] = = First ++i) {if (!used[i]) {Stack[index] = i;used[i] = true;if (DFS (Str[i][s[i]-1], Index + 1)) return true;used[i] = false;}} return false;} int main () {int t;int i,first;scanf ("%d", &t), while (t--) {scanf ("%d", &n), for (i = 0; i < n; ++i) scanf ("%s", Str[i ]); memset (used, 0, sizeof (used)), memset (in, 0, sizeof (in)), memset (out, 0, sizeof), qsort (str, N, * sizeof (char), C Ompare); String sort for (i = 0; i < n; ++i)//extract from the letter of entry and exit (S[i] = strlen (Str[i]); ++out[str[i][0]-' a '];++in[str[i][s[i]-1]-' a ']; }first = Judge (); Determine the starting point if (first! =-1 && DFS (first + ' a ', 0)) {for (i = 0; i < n-1; ++i) printf ("%s.", Str[stack[i]);p rintf ("%s\ N ", Str[stack[n-1]]);} elseprintf ("***\n");}}
C Language DFS (7) ___ Word stitching (nyoj 99)