题干
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 | 4 |
Output
1 | The highest possible quality is 1/3. |
题意与思路
题意可以转化为:
有若干个长度为 7 的字符串,定义两个字符串之间的距离为它们不同的位数,例如:
1 | str1 = "aaaaaaa" |
其中:
str1
与str2
只有第一位不同,故二者的距离为 1;str1
与str3
有两位不同,故二者的距离为 2;
假设所有字符串都是由同一个字符串演变而来,求出最少的演变距离,例如以 str1
为演变起点:
- 要得出
str2
,由str1
演变而来,所需的距离为1
- 要得出
str3
,相比于str1
,从str2
演变至str3
的距离更短,距离为1
得出最少的演变距离总和 Q=2
,输出语句为:
1 | The highest possible quality is 1/2. |
可以理解为:在一个完全图中,找到一颗包含所有节点的树,使得树的边权最小,本质上是一道最小生成树问题。其基本思想为:从一个随机的起点开始构建树,不断地寻找距离当前树最近的新节点加入树,直到所有节点都包含在树中。
对于本题而言,其基本流程为:
- 定义一个数组
minDis
用于存储当前树到其他节点的最短距离; - 随机选取一个起点,将其标记为已访问,并将其所有边传入数组
minDis
; - 通过数组
minDis
找到距离当前树最近的新节点,将该节点标记为已访问,使用该节点的所有边更新数组minDis
; - 重复步骤 3 直至所有节点都被访问;
题解
1 |
|