Truck History
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 15977 | Accepted: 6130 |
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 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.
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. 看起来很吓人实际上就是最小生成树的裸题,大致意思是求出任意两个字符串中不同的字母数作为两个字符串之间的权值,因此我们得到的是一个无向完全图,然后在此基础上 求出最小生成树便可以了。当然分析了下这道题的复杂度一开始本来想用KLUSKAL做的 发现复杂度ElogE比较危险,所以姑且复习一下PRIM并用PRIM过了。#include<cstdio> #include<cstring> #include<cmath> #include<iostream> #include<algorithm> using namespace std; const int inf=10; int n; int ans; char str[2001][8]; int map[2001][2001]; int dis[2001]; bool visited[2001]; int prim() { int i,j,pos,min; ans=0; memset(visited,0,sizeof(visited)); visited[1]=1;pos=1;//从某点开始,分别标记和记录该点,默认从1开始 for(i=1;i<=n;i++)//第一次给dis数组赋值 if(i!=pos) dis[i]=map[pos][i]; for(i=1;i<n;i++)//再运行n-1次 { min=inf; for(j=1;j<=n;j++) if(visited[j]==0&&min>dis[j]) { min=dis[j];pos=j;//找出最小权值并记录位置 } ans+=min;//最小权值累加 visited[pos]=1;//标记该点 for(j=1;j<=n;j++) if(visited[j]==0&&dis[j]>map[pos][j]) dis[j]=map[pos][j];//更新权值 } return ans; } int main() { int i,j,k; while(cin>>n) { if(n==0) return 0; memset(map,inf,sizeof(map)); for(i=1;i<=n;i++) { cin>>str[i]; for(j=i-1;j>=1;j--) { int edge=0; for(k=0;k<7;k++) if(str[i][k]!=str[j][k])//求出任意两个字符串间的权值 edge++; map[i][j]=map[j][i]=edge; } } prim(); printf("The highest possible quality is 1/%d.\n", ans); } return 0; }