抱歉,您的浏览器无法访问本站
本页面需要浏览器支持(启用)JavaScript
了解详情 >

题干

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 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.

先进货物运输有限公司使用不同类型的卡车。有些卡车用来运送蔬菜,有些卡车用来运送家具或砖块。该公司有自己的代码来描述每种类型的卡车。代码只是一个由七个小写字母组成的字符串(每个位置上的每个字母都有一个非常特殊的含义,但这对于本任务来说并不重要)。在公司历史的开始,只使用一种卡车类型,但后来从它派生出其他类型,然后从新的类型派生出其他类型,等等。

如今,美国计算机协会已经有钱请历史学家来研究它的历史。历史学家试图找出的一件事是所谓的衍生计划,即卡车的类型是如何衍生出来的。他们将卡车类型的距离定义为卡车类型代码中具有不同字母的位置的数量。他们还假设每一种卡车类型都是从另一种卡车类型派生出来的(除了第一种卡车类型,它不是从任何其他类型派生出来的)。派生计划的质量定义为: 对派生计划中的所有类型对求和,其中to为原始类型,td为派生类型,d(to,td)为类型之间的距离。 既然历史学家失败了,你要写一个程序来帮助他们。给定卡车类型的代码,您的程序应该找到派生计划的最高质量。


Input

输入由几个测试用例组成。每个测试用例以一行开头,其中包含卡车类型的数量,N, 2 <= N <= 2000。以下N行输入中的每一行都包含一个卡车类型代码(由七个小写字母组成的字符串)。您可以假设代码唯一地描述了卡车,也就是说,这N行中没有两行是相同的。输入在卡车类型数处以零结束。

Output

对于每个测试用例,您的程序应该输出文本“最高可能的质量是1/Q”,其中1/Q是最佳派生计划的质量。

测试案例

Input

1
2
3
4
5
6
4
aaaaaaa
baaaaaa
abaaaaa
aabaaaa
0

Output

1
The highest possible quality is 1/3.

题意与思路

题意可以转化为:

有若干个长度为 7 的字符串,定义两个字符串之间的距离为它们不同的位数,例如:

1
2
3
str1 = "aaaaaaa"
str2 = "baaaaaa"
str3 = "baaacaa"

其中:

  • str1str2 只有第一位不同,故二者的距离为 1;
  • str1str3 有两位不同,故二者的距离为 2;

假设所有字符串都是由同一个字符串演变而来,求出最少的演变距离,例如以 str1 为演变起点:

  • 要得出 str2 ,由 str1 演变而来,所需的距离为 1
  • 要得出 str3 ,相比于 str1,从 str2 演变至 str3 的距离更短,距离为 1

得出最少的演变距离总和 Q=2,输出语句为:

1
The highest possible quality is 1/2.

可以理解为:在一个完全图中,找到一颗包含所有节点的树,使得树的边权最小,本质上是一道最小生成树问题。其基本思想为:从一个随机的起点开始构建树,不断地寻找距离当前树最近的新节点加入树,直到所有节点都包含在树中。

对于本题而言,其基本流程为:

  1. 定义一个数组 minDis 用于存储当前树到其他节点的最短距离
  2. 随机选取一个起点,将其标记为已访问,并将其所有边传入数组 minDis
  3. 通过数组 minDis 找到距离当前树最近的新节点,将该节点标记为已访问,使用该节点的所有边更新数组 minDis
  4. 重复步骤 3 直至所有节点都被访问;

题解

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <iostream>
#include <cstring>
#include <cstdio>

using namespace std;

const int maxn = 2010;

int edge[maxn][maxn]; // 用于记录边权
int minDis[maxn]; // 用于记录当前树距离其他各个节点的最短距离
bool isVisit[maxn]; // 用于记录节点是否访问过
char str[maxn][10]; // 用于存储字符串
int N;

// 计算完全图的边权
void calculateEdge(){
int cnt;
for(int i = 0;i < N;i++)
for(int j = i + 1;j < N;j++){
cnt = 0;
for(int k = 0;k < 7;k++)
if(str[i][k] != str[j][k])
cnt++;
edge[i][j] = edge[j][i] = cnt;
}
}

// 访问节点index时,更新最短距离表minDis
void updateMinDis(int index){
for(int i = 0;i < N;i++)
minDis[i] = min(minDis[i], edge[index][i]);

}

int main(){

while (~scanf("%d", &N)){
if(N == 0) break;
memset(isVisit, 0, sizeof(isVisit));
for(int i = 0;i < N;i++)
scanf("%s", str[i]);
calculateEdge(); // 计算完全图的边权

int sumDistance = 0;

isVisit[0] = true; // 初始节点标记已访问
int visited = 1; // 记录已访问的节点数

for(int i = 0;i < N;i++) // 记录初始节点与其他所有节点之间的距离
minDis[i] = edge[0][i];

while (visited < N){
int minDistance = 7; // 距离当前树最近的节点的距离
int closestNode = 0; // 距离当前树最近的节点的下标

// 查找当前距离树最近的节点,记录其距离和下标
for(int i = 0;i < N;i++){
if(isVisit[i]) continue;
if(minDis[i] <= minDistance){
minDistance = minDis[i];
closestNode = i;
}
}

isVisit[closestNode] = true; // 将与当前树距离最近的节点标记为已访问
sumDistance += minDistance;
visited ++;
updateMinDis(closestNode);
}

printf("The highest possible quality is 1/%d.\n", sumDistance);

}

return 0;
}

评论