Truck history
Time Limit: 2000MS |
|
Memory Limit: 65536K |
Total Submissions: 20850 |
|
Accepted: 8065 |
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.
Source
CTU Open 2003
1#include <cstdio>2#include <cstring>3#include <string>4#include <queue>5#include <stack>6#include <iostream>7 using namespacestd;8 strings[2005];9 intmap[2005][2005];Ten intdis[2005]; One intGetdiff (stringS1,stringS2) { A inti,num=0; - for(i=0;i<7; i++){ - if(s1[i]!=S2[i]) { thenum++; - } - } - returnnum; + } - intMain () { + //freopen ("D:\\input.txt", "R", stdin); A intN; at while(SCANF ("%d", &n)! =EOF) { - if(!N) { - Break; - } - inti,j; - for(i=0; i<n;i++){ inCin>>S[i]; - } tomemset (DIS,8,sizeof(DIS)); + for(i=0; i<n;i++){ - for(j=i;j<n;j++){ themap[j][i]=map[i][j]=Getdiff (S[i],s[j]); * } $dis[i]=map[0][i];Panax Notoginseng } - intmink,min,sum=0; the for(i=0; i<n-1; i++){ +min=8; A for(j=0; j<n;j++){ the if(dis[j]&&min>Dis[j]) { +min=Dis[j]; -mink=J; $ } $ } -sum+=Dis[mink]; - for(j=0; j<n;j++){ the if(dis[j]>Map[mink][j]) { -dis[j]=Map[mink][j];Wuyi } the } -dis[mink]=0; Wu } -cout<<"The highest possible quality is 1/"<<sum<<"."<<Endl; About } $ return 0; -}
POJ 1789 Truck History