Directory
1 Problem Description
2 Solutions
1 problem description
Description
a catenym is a pair of words separated by A period such the last letter of the first word is the same as the Las T letter of the second. For example, the following is catenyms:
Dog.gopher
Gopher.rat
Rat.tiger
Aloha.aloha
Arachnid.dog
a compound catenym is a sequence of three or more words separated by periods such so each adjacent pair of words for Ms a catenym. For example,
Aloha.aloha.arachnid.dog.gopher.rat.tiger
Given A dictionary of lower case words, you is to find a compound catenym This contains each of the words exactly onc E.
Input
the first line of standard input contains T, the number of test cases. Each test case is begins with 3 <= n <= 1000-the number of words in the dictionary. n distinct dictionary words follow; Each of the word is a string of between 1 and lowercase letters in a line by itself.
Output
for each test case, output a line giving the lexicographically least compound catenym This contains each dictionary wo Rd exactly once. Output "* * *" If there is no solution.
Sample Input
26alohaarachniddoggopherrattiger3oakmapleelm
Sample Output
aloha.arachnid.dog.gopher.rat.tiger***
Source
Waterloo Local 2003.01.25title Link: http://poj.org/problem?id=2337
2 Solutions
The specific code is as follows:
PackageCom.liuzhen.practice;Importjava.util.ArrayList;Importjava.util.Collections;ImportJava.util.Comparator;ImportJava.util.Scanner; Public classMain { Public Static intMAX = 30;//a total of 26 English letters@SuppressWarnings ("Unchecked") Public Staticarraylist<edge>[] Map =NewArraylist[max]; Public Staticstring[] path; Public Static intCount//count the number of edges accessed by the DFS traversal, that is, the connectivity of the detection graph Public Static intstart; Public Static int[] Indegree =New int[MAX]; Public Static int[] Outdegree =New int[MAX]; Public Staticarraylist<string> result =NewArraylist<string>(); classMycomparatorImplementsComparator<edge> { Public intCompare (Edge arg0, Edge arg1) {String A=Arg0.word; String B=Arg1.word; intJudge =A.compareto (B); if(Judge > 0) return1; Else if(Judge < 0) return-1; return0; } } Static classEdge { Public intA//the first letter ordinal of a word Public intb//The last letter ordinal of a word PublicString Word;//specific Words Public BooleanUsed;//determine if a word is accessed PublicEdgeintAintB, String Word) { This. A =A; This. B =b; This. Word =Word; Used=false; } } Public voidInitintk) {Start=MAX; for(inti = 0;i < max;i++) {Map[i]=NewArraylist<edge>(); Indegree[i]= 0; Outdegree[i]= 0; } Path=NewString[k]; for(inti = 0;i < k;i++) Path[i]= ""; Count= 0; } Public BooleanJudgedegree () {intin = 0, out = 0; for(inti = 1;i < map.length;i++) {//Dictionary ordering of words in Map[i] if(Map[i].size () > 1) Collections.sort (Map[i],Newmycomparator ()); } for(inti = 0;i < indegree.length;i++) { if(Indegree[i] = =Outdegree[i])Continue; Else if(Indegree[i]-outdegree[i] = = 1) in++; Else if(Outdegree[i]-indegree[i] = = 1) { out++; Start= i;//at this point, there may be Euler paths, which must be traversed from a point in which the degree is less than the degree of}Else return false; } if(in = = out && (in = = 0 | | in = = 1)) return true; return false; } Public voidDfsintbegin) { for(inti = 0;i < Map[begin].size (); i++) {Edge temp=Map[begin].get (i); if(temp.used = =false) {temp.used=true; Path[count++] =Temp.word; DFS (TEMP.B); } } } Public Static voidMain (string[] args) {main test=NewMain (); Scanner in=NewScanner (system.in); intt =In.nextint (); while(T > 0) {T--; intK =In.nextint (); Test.init (k); for(inti = 0;i < k;i++) {String A=In.next (); intA = A.charat (0)-' a '; intb = A.charat (A.length ()-1)-' A '; Start=math.min (Start, Math.min (A, b)); Map[a].add (NewEdge (A, B, a)); Outdegree[a]++; INDEGREE[B]++; } StringBuilder Temp=NewStringBuilder (""); if(Test.judgedegree ()) {//satisfies the requirements for the vertex degree of the Euler circuit or the Euler pathTest.dfs (start); if(count = = k) {//Diagram Connectivity for(inti = 0;i < k;i++) {temp.append (path[i]); if(I! = k-1) Temp.append ("."); } } Else{temp.append ("***"); } } Else{temp.append ("***"); } result.add (Temp.tostring ()); } for(inti = 0;i < Result.size (); i++) System.out.println (Result.get (i)); }}
Operation Result:
Alohaarachniddoggopherrattiger3oakmapleelmaloha.arachnid.dog.gopher.rat.tiger* * *
Resources:
1. Euro-Pull circuit
Algorithm Note _148: Tuola loop Solver (Java)