Truck History
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 21535   Accepted: 8380

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 (except for the first truck type which was not derived from any other type). The quality of a derivation plan was then defined as
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 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). 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 should output the text "The highest possible quality is 1/Q.", where 1/Q is the quality of the best derivation plan.

Sample Input

4
aaaaaaa
baaaaaa
abaaaaa
aabaaaa
0

Sample Output

The highest possible quality is 1/3.

Source

RE: 题目真长 → → "RT"。 就是说给几个串, 串与串之间有些字母不同, 求任取两串最小不同字母数之和; 可以转化成最小生成树理解, 任取两串, 串的编号做点, 两串不同字母数做权值, 转化成了求最小生成树问题,
 1 #include <cstdio>
 2 #include <cstring>
 3 #include <iostream>
 4 using namespace std;
 5 int map[2020][2020], dis[2020]; bool vis[2020];
 6 const int INF = 0x3f3f3f3f;
 7 char str[2020][8];
 8 int n;
 9 int Deal(int a, int b)   //求权值;
10 {
11     int m = 0;
12     for(int i = 0; i < 7; i++)
13         if(str[a][i] != str[b][i])
14             m++;
15     return m;
16 }
17 void Prim()
18 {
19     int sum = 0;
20     memset(vis, true, sizeof(vis));
21     for(int i = 1; i <= n; i++)
22         dis[i] = map[1][i];
23     vis[1] = false;
24     for(int i = 1; i < n; i++)
25     {
26         int temp, min = INF;
27         for(int j = 1; j <= n; j++)
28         {
29             if(vis[j] && dis[j] < min)
30             {
31                 min = dis[j];
32                 temp = j; 
33             }
34         }
35         if(min == INF)
36             return ;
37         vis[temp] = false;
38         sum += min;
39         for(int j = 1; j <= n; j++)
40             if(vis[j] && dis[j] > map[temp][j])
41                 dis[j] = map[temp][j];
42     }
43     printf("The highest possible quality is 1/%d.\n", sum);
44 }
45 int main()
46 {
47     while(~scanf("%d", &n), n)
48     {
49         for(int i = 1; i <= n; i++)
50             scanf("%s", str[i]);
51         for(int i = 1; i <= n; i++)
52         {
53             map[i][i] = 0;
54             for(int j = 1; j < i; j++)
55                 map[i][j] = map[j][i] = Deal(i, j);
56         }
57         Prim();
58     }
59     return 0;
60 }

 

posted on 2015-08-11 19:29  cleverbiger  阅读(195)  评论(0编辑  收藏  举报