Description
Advanced Cargo Movement, Ltd. uses trucks of different types. some trucks are used for vegetable delivery, other for furniture, or for bricks. the company has its own code describing each type of a truck. the code is simply a string of exactly seven lowercase letters (each letter on each position has a very special meaning but that is unimportant for this task ). at the beginning of company's history, just a single truck type was used but later other types were derived from it, then from the New Types another types were derived, and so on.
Today, ACM is rich enough to pay historians to study its history. one thing historians tried to find out is so called derivation plan -- I. e. how the truck types were derived. they defined the distance of truck types as the number of positions with different letters in truck type codes. they also assumed that each truck type was derived from exactly one other truck type (role t for the first truck type which was not derived from any other type ). the quality of a derivation plan was then defined
1/Σ (to, TD) D (to, TD)
Where the sum goes over all pairs of types in the derivation plan such that to is the original type and TD the type derived from it and D (to, TD) is the distance of the types.
Since historians failed, you are to write a program to help them. Given the codes of truck types, your program shocould find the highest possible quality of a derivation plan.
Input
The input consists of several test cases. each test case begins with a line containing the number of truck types, N, 2 <= n <= 2 000. each of the following n lines of input contains one truck type code (a string of seven lowercase letters ). you may assume that the codes uniquely describe the trucks, I. E ., no two of these n lines are the same. the input is terminated with zero at the place of number of truck types.
Output
For each test case, your program shocould output the text "the highest possible quality is 1/Q.", where 1/Q is the quality of the best derivation plan.
Sample Input
4aaaaaaabaaaaaaabaaaaaaabaaabaaaa0
Sample output
The highest possible quality is 1/3. A question is abstract.7The character string represents a number.DistanceRepresents the number of different letters between the two numbers. One serial number can only be assigned by another serial number."Derivative"Come out, the cost is the corresponding between the two numbersDistanceTo find"Derivative"Solution to minimize the total cost, that isDistanceThe sum is the smallest. The difficulty lies in converting abstract concepts into graphs. Code:# Include <iostream> # include <cstdio> using namespace STD; # define Max 2005 char map [Max] [20]; int MAP2 [Max] [Max]; // Save the (distance) int vis [Max]; int dis [Max]; int value (int x, int y) {int S = 0; for (INT I = 0; I <7; I ++) if (Map [x] [I]! = Map [y] [I]) {s ++;} return s;} int prim (int n) {int S = 0, POs, I, j, min1; memset (VIS, 0, sizeof (VIS); for (I = 0; I <n; I ++) dis [I] = MAP2 [0] [I]; // assume that the minimum distance is the sum of the distance from string 0 to all other strings vis [0] = 1; for (I = 0; I <n-1; I ++) {min1 = max; for (j = 0; j <n; j ++) {If (vis [J] = 0 & dis [J] <min1) /// connect a shortest {min1 = dis [J]; Pos = J ;}} vis [POS] = 1; S = S + dis [POS]; for (j = 0; j <n; j ++) {If (vis [J] = 0 & dis [J]> MAP2 [POS] [J]) /// check whether POs to J is greater than 0 to J in other routes. Dis [J] = MAP2 [J] [POS] ;}} return s ;} int main () {int N, I, j; while (scanf ("% d", & N), n) {for (I = 0; I <n; I ++) scanf ("% s", map [I]); for (I = 0; I <n-1; I ++) for (j = I + 1; j <N; j ++) // the distance between each two strings. MAP2 [I] [J] = MAP2 [J] [I] = value (I, j ); int Ss = prim (n); printf ("the highest possible quality is 1/% d. \ n ", SS) ;}return 0 ;}
Poj 1789 truck history Minimum Spanning Tree