poj1789 Truck History
来源:互联网 发布:pptv网络电视官网 编辑:程序博客网 时间:2024/06/05 00:08
Truck History
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 21523 Accepted: 8372
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.
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.
Sample Input
4aaaaaaabaaaaaaabaaaaaaabaaaa0
Sample Output
The highest possible quality is 1/3.
题目不太好理解:
直接解释了:题目讲的是字符串之间的“衍生”,如何“衍生”使字符串变化量最小;
例如:aaaaaaa 衍生 baaaaaa 需要 1个变化量
baaaaaa 衍生 abaaaaa 需要 2个变化量
所以用aaaaaaa对其他三个进行“衍生”所需的变化量最小;
具体看代码:
克鲁斯卡尔算法:
#include<stdio.h>#include<algorithm>using namespace std;#include<string.h>char s[2005][8];int set[2005];struct line{int begin;int end;int cost;}num[2000000];int cmp(line a,line b){return a.cost <b.cost ;}int dis(int i,int j)//字符串进行对比,看差别量! {int k=0,t=0;for(k=0;k<7;k++) { if(s[i][k]!=s[j][k]) t++; } return t;}int find(int p){int t;int child=p;while(p!=set[p])p=set[p];while(child!=p){t=set[child];set[child]=p;child=t;}return p;}bool merge(int x,int y){int fx=find(x);int fy=find(y);if(fx!=fy){set[fx]=fy;return true;}elsereturn false;}int main(){int n;while(scanf("%d",&n),n){int i,j;for(i=0;i<n;i++)//将字符串存入二维字符串中 { set[i]=i; scanf("%s",s[i]); } int t=0; for(i=0;i<n;i++) for(j=i+1;j<n;j++) { num[t].begin =i; num[t].end =j; num[t].cost =dis(i,j); t++; } int outcome=0; sort(num,num+t,cmp); for(i=0;i<t;i++) { if(merge(num[i].begin ,num[i].end ))//判断是否成环 outcome+=num[i].cost ; } printf("The highest possible quality is 1/%d.\n",outcome);}return 0;}
1 0
- POJ1789 Truck History
- Truck History(POJ1789)
- poj1789 - Truck History
- Truck History poj1789
- poj1789 Truck History
- poj1789--Truck History
- poj1789 Truck History
- poj1789 Truck History(Prim)
- POJ1789 Truck History【Prim】
- POJ1789--Truck History
- poj1789 Truck History
- POJ1789 Truck History
- POJ1789 Truck History
- POJ1789-Truck History .
- POJ1789 Truck History
- POJ1789 Truck History
- POJ1789 Truck History
- POJ1789 Truck History
- linux服务器下tomcat部署项目内存溢出
- iOS8 xcode6中添加pch全局引用文件
- hdu 5307 He is Flying
- I学霸官方免费教程二十三:Java常用类之日期类 Date类 SimpleDateFormat类 Calendar类
- 【HDOJ 5375】 Gray code
- poj1789 Truck History
- 通过<ListView>将List<Object>里的属性显示出来
- DayDayUP_Linux运维学习_linux下Tomcat的安装(包含jdk的安装)
- hadoop学习之hadoop完全分布式集群安装
- LeetCode之Balanced Binary Tree
- jsp自定义标签
- java面向对象之_类Class
- JAVA-WEB服务器
- 浏览器兼容解决方案