Truck History
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 21534   Accepted: 8379

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.

这题,我是没看懂,后来看的别人的翻译才自己做的!
 > 题意大概是这样的:用一个7位的string代表一个编号,两个编号之间的distance代表这两个编号之间不同字母的个数。一个编号只能由另一个编号“衍生”出来,代价是这两个编号之间相应的distance,现在要找出一个“衍生”方案,使得总代价最小,也就是distance之和最小。

> 例如有如下4个编号:

> aaaaaaa

> baaaaaa

> abaaaaa

> aabaaaa

> 显然的,第二,第三和第四编号分别从第一编号衍生出来的代价最小,因为第二,第三和第四编号分别与第一编号只有一个字母是不同的,相应的distance都是1,加起来是3。也就是最小代价为3。

显然,最小生成树!
 #include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; int per[],n;
struct node
{
int b,e,w;
}s[];//我开的挺大的,怕不够
bool cmp(node x,node y)
{
return x.w<y.w;
}
void init()
{
for(int i=;i<=n;i++)
per[i]=i;
}
int find(int x)
{
int i=x,j;
while(x!=per[x])
x=per[x];
while(i!=x)//我压缩了路径踩过的,超时了好多次
{
j=per[i];
per[i]=x;
i=j;
}
return x;
}
bool join(int x,int y)
{
int fx=find(x);
int fy=find(y);
if(fx!=fy)
{
per[fx]=fy;
return true;
}
return false;
}
int main()
{
char map[][];
while(scanf("%d",&n),n)
{
getchar();
init();
int i,j;
for(int i=;i<n;i++)
scanf("%s",map[i]);
int k=,sum=,t;
for(i=;i<n;i++)
{
for(j=i+;j<n;j++)
{
int cot=;
for(t=;t<;t++)
{
if(map[i][t]!=map[j][t])
cot++;
}
s[k].b=i;
s[k].e=j;
s[k].w=cot;
k++;
}
}
sort(s,s+k,cmp);
for(i=;i<k;i++)
{
if(join(s[i].b,s[i].e))
sum+=s[i].w;
}
printf("The highest possible quality is 1/%d.\n",sum);
}
return ;
}
欢迎留言。

最新文章

  1. [转] js实现html table 行,列锁定
  2. 51nod1085(01背包)
  3. 【转】Crontab定时任务配置
  4. AC日记——石头剪子布 openjudge 1.7 04
  5. 隐匿在iOS文件系统中的隐私信息
  6. centos6.6下编译安装mysql5.6之后启动失败:Starting MySQL... ERROR! The server quit without updating PID file (/var/lib/mysql/localhost.localdomain.pid).
  7. Docker 使用指南 (六)—— 使用 Docker 部署 Django 容器栈
  8. libGraphicsMagickWand.so: cannot open shared object file: No such file or directory stack traceback:
  9. CygWin模拟Linux环境进行Ant批量打包
  10. HIbernate学习笔记(八) hibernate缓存机制
  11. (hdu step 7.1.2)You can Solve a Geometry Problem too(乞讨n条线段,相交两者之间的段数)
  12. vue-router2.0动态路由获取参数
  13. Rational Rose正逆向工程(类图转Java代码,Java代码转类图)
  14. 你应该学会的Python多版本管理工具Pyenv
  15. js调用app启动页
  16. 【pygame游戏编程】第二篇-----移动图像
  17. webvirtmgr-重命名kvm虚拟机的名称
  18. xml文档的读取
  19. Linux VMware安装CentOS
  20. [剑指Offer]50-第一个只出现一次的字符

热门文章

  1. C程序设计语言练习题1-1
  2. VC维
  3. jquery.mmenu
  4. POJ 1755 Triathlon
  5. 深入理解7816(5)-----关于文件DF/EF/MF/FID/AID/SFI
  6. JVM JMM
  7. tyvj1039忠诚2
  8. DL,DT,DD,比传统table更语义,解析更快的table列表方式
  9. Http(get,post)及HttpClient(get,post)的简单使用
  10. linux系统批量无人值守安装