Truck history
Description
Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks is used for vegetable delivery, and for furniture, or for bricks. The company have its own code describing each type of a truck. The code is simply a string of exactly seven lowercase letters (all letter in each position have a very special meaning bu T is the unimportant for this task). At the beginning of company's history, just a single truck type is used but later other types were derived from it, then From the new types another types were derived, and so on.
Today, ACM is a rich enough to pay historians to study it history. One thing historians tried to find out are 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 is derived from exactly one other truck type (except for the first truck type whic H is not derived from any other type). The quality of a derivation plan was and then defined as
1/σ (TO,TD) d (TO,TD)
Where the sum goes over all pairs of types in the derivation plan such, that's the original type and TD the type derive D from it and D (TO,TD) is the distance of the types.
Since historians failed, you is to the write a program to the help them. Given The codes of truck types, your program should 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). Assume that the codes uniquely describe the trucks, i.e., No. Of these N lines is the same. The input is terminated with zero at the place of number of truck types.
Output
For each test case, your program should output the text "the highest possible quality was 1/q.", where 1/q is the quality O f the best derivation plan.
Sample Input
4aaaaaaabaaaaaaabaaaaaaabaaaa0
Sample Output
The highest possible quality is 1/3.
1#include <cstdio>2#include <cstring>3 using namespacestd;4 5 structnode6 {7 Chars[8];8}p[2005];9 Ten intlowercost[2005]; One intvis[2005]; A intN; - Const intINF =0x3f3f3f3f; - the intCaldist (Char*S1,Char*S2) - { - intCnt=0; - for(intI=0;i<7; i++) + if(s1[i]!=S2[i]) -cnt++; + returnCNT; A } at - intPrim () - { - intsum=0; - inti,j; - for(i=1; i<n;i++) inLowercost[i]=caldist (p[0].s,p[i].s); -vis[0]=true; to for(i=1; i<n;i++) + { - inttemp=inf; the intu; * for(j=0; j<n;j++) $ {Panax Notoginseng if(!vis[j]&&lowercost[j]<temp) - { thetemp=Lowercost[j]; +u=J; A } the } +vis[u]=true; -sum+=temp; $ for(j=0; j<n;j++) $ { - intdist=caldist (P[J].S,P[U].S); - if(!vis[j]&&dist<Lowercost[j]) thelowercost[j]=Dist; - }Wuyi } the returnsum; - } Wu - intMain () About { $ while(SCANF ("%d",&N), N) - { -memset (Lowercost,inf,sizeof(Lowercost)); -memset (Vis,0,sizeof(Vis)); A for(intI=0; i<n;i++) +scanf"%s", P[I].S); the intans=Prim (); -printf"The highest possible quality is 1/%d.\n", ans); $ } the return 0; the}
POJ 1789 Truck history