POJ
来源:互联网 发布:富士 打印软件 编辑:程序博客网 时间:2024/06/08 00:07
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 t o is the original type and t d the type derived from it and d(t o,t d) 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.
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 t o is the original type and t d the type derived from it and d(t o,t d) 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.
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.
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.
4aaaaaaabaaaaaaabaaaaaaabaaaa0
The highest possible quality is 1/3.
#include <iostream>#include<stdio.h>#include<string.h>#include<algorithm>#define INF 0x3f3f3f3fusing namespace std;/*以后注意边的数目问题,算准了,在去写,不要错在这些小地方*/const int maxn=2010;char str[2010][8];int pre[maxn];struct edge{ int u,v; int w;}Edge[maxn*maxn];int find_root(int x){ return pre[x]==x?x:pre[x]=find_root(pre[x]);}int cmp(edge a,edge b){ return a.w<b.w;}int main(){ int n; while(scanf("%d",&n)&&n) { memset(pre,0,sizeof(pre)); memset(Edge,0,sizeof(Edge)); memset(str,0,sizeof(str)); for(int i=0;i<=n;i++) pre[i]=i; for(int i=0;i<n;i++) { getchar(); scanf("%s",str[i]); } int cnt=0; for(int i=0;i<n;i++) for(int j=i+1;j<n;j++) { int num=0; if(str[i][0]!=str[j][0]) num++; if(str[i][1]!=str[j][1]) num++; if(str[i][2]!=str[j][2]) num++; if(str[i][3]!=str[j][3]) num++; if(str[i][4]!=str[j][4]) num++; if(str[i][5]!=str[j][5]) num++; if(str[i][6]!=str[j][6]) num++; Edge[cnt].u=i,Edge[cnt].v=j,Edge[cnt++].w=num; } int ans=0; sort(Edge,Edge+cnt,cmp); for(int i=0;i<cnt;i++) { int x=Edge[i].u,y=Edge[i].v; int rx=find_root(x),ry=find_root(y); if(rx!=ry) { pre[ry]=rx; ans += Edge[i].w; } } printf("The highest possible quality is 1/%d.\n",ans); } return 0;}
阅读全文
0 0
- POJ
- poj
- POJ
- POJ
- poj
- poj
- POJ
- POJ
- poj
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- c#简单使用IBM-MO编程
- OPENWRT入门之一------openwrt简介(转载)
- 2017年8月7日提高组T1 呵呵
- springboot(六):如何优雅的使用mybatis
- 欢迎使用CSDN-markdown编辑器
- POJ
- 相对布局的layout_alignLeft属性
- js清空input file的值
- 计算机视觉领域的一些好的地址链接
- POJ 3254 Corn Fields(状压DP)
- csrf攻击
- 同时配置redis和hbase的配置文件遇到的问题
- python中zip函数
- python 进程间通信