ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

POJ 1789

2019-08-06 19:57:02  阅读:255  来源: 互联网

标签:pre 1789 int pos POJ truck type types


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. 

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.
题目的难点在于题意不好理解,,还有就是 树很难搭建
题目大意:就是 对于输入的字符串,,已行尾单位,比如说第一行和第二行在相同位置有1个字符不同那么 起点为1,终点为2,权值为1 即权值就是不同字符的个数

//编写程序输出最高质量的火车推导计划。火车推倒计划质量的求解公式中的分子为1,分母为t0,td的距离总和。
//t0,td的距离就是两个输入字符串的字母不同位置的个数。想要推导计划的质量最高就是使分母最小。即t0,td的距离总和最小。
//实质上是以任意两个字符串为结点,以字符串字母不同位置的个数为两节点之间边的权值,求解最小生成树。
//因此输入和结点联系密切,使用kruskal算法求解比较方便。
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int N=1e5+7;
const int M=7e7+7;
char s[N][10];
struct stu{
int a,b,c;
}p[M];

int pre[N];

bool cmp(stu x,stu y){
return x.c<y.c;
}

int find(int x){
int r=x;
while(r!=pre[r])
r=pre[r];
int k=x,j;
while(k!=r){
j=pre[k];
pre[k]=r;
k=j;
}
return r;
}
int main(){
int n;
while(~scanf("%d",&n)&&n){
for(int i=0;i<=n;i++){
pre[i]=i;
}
for(int i=0;i<n;i++)
scanf("%s",s[i]);
int pos=0;
//重点 树的搭建
for(int i=0; i<n; i++)//第i行
for(int j=n-1; j>i; j--)//从第n-1行到第i行寻找不同的字符个数 ,并用结构体保存
{
int sum=0;
for(int x=0; x<7; x++)
if(s[i][x]!=s[j][x])
sum++;
p[pos].a=i;
p[pos].b=j;
p[pos].c=sum;
pos++;
}

sort(p,p+pos,cmp);

int ans=0;
for(int i=0;i<pos;i++){
int fx=find(p[i].a);
int fy=find(p[i].b);
if(fx!=fy){
pre[fx]=fy;
ans+=p[i].c;
}
}

printf("The highest possible quality is 1/%d.\n",ans);
}
return 0;
}

 

标签:pre,1789,int,pos,POJ,truck,type,types
来源: https://www.cnblogs.com/Accepting/p/11311370.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有