Truck History
Time Limit: 2000MS | | Memory Limit: 65536K |
Total Submissions: 21508 | | Accepted: 8361 |
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.
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.
//克鲁斯卡尔算法
/*
题意::
给你n个车,每个车用7个字母表示,他们的差距可以用它们不同字母的个数表示,现在,要先找出它们
差距和的最小值。然后它们的最小和的倒数即为所求的最大值。
*/
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int a[2100];
char b[2100][20];//用来存储字符串
int sum;
struct zz
{
int f1,f2,d;
}s[4000010];
int cmp(zz x,zz y)//按差距从小到大排序
{
return x.d<y.d;
}
int find(int x)
{
while(x!=a[x])
x=a[x];
return x;
}
int marge(int x,int y)
{
int fx,fy;
fx=find(x);
fy=find(y);
if(fx!=fy)
{
a[fx]=fy;
return 1;
}
return 0;
}
int bj(char s1[],char s2[])//比较两个字符串不同字母的个数
{
int cnt=0,i;
for(i=0;i<7;i++)
{
if(s1[i]!=s2[i])
cnt++;
}
return cnt;
}
int main(){
int t,i,j,l,k,n;
while(scanf("%d",&t),t)
{
for(i=1;i<=t;i++)
a[i]=i;
for(i=1;i<=t;i++)
scanf("%s",b[i]);
k=0;
for(i=1;i<t;i++) //重点:记录两个串不同字母的个数
for(j=1+i;j<=t;j++)
{
s[k].f1=i;s[k].f2=j;
s[k++].d=bj(b[i],b[j]);
}
sort(s,s+k,cmp);
sum=0;n=0;
for(i=0;i<k;i++)
{
if(marge(s[i].f1,s[i].f2))
{
sum+=s[i].d;
n++;//重点:用来判定是否已经成树,
}
if(n==t-1)//树的定义,n个点,n-1条边,如果已经完成,后面的即无需在加,节省时间,WA了两次
break;
}
if(n==t-1)
printf("The highest possible quality is 1/%d.\n",sum);
}
return 0;
}
<pre class="cpp" name="code">//prim算法
#include<stdio.h>
#include<string.h>
#define mx 0x3f3f3f3f
char b[2100][20];
int n;
int g[2010][2010],vis[2100],dis[2100];
int bj(char s1[],char s2[])
{
int i,cnt=0;
for(i=0;i<7;i++)
{
if(s1[i]!=s2[i])
{
cnt++;
}
}
return cnt;
}
void prim()
{
int i,j,k,min,sum=0;
memset(vis,0,sizeof(vis));
for(i=1;i<=n;i++)
{
dis[i]=g[1][i];
}
dis[1]=0;
vis[1]=1;
for(j=1;j<n;j++)
{
min=mx;k=1;
for(i=1;i<=n;i++)
{
if(!vis[i]&&dis[i]<min)
{
min=dis[i];
k=i;
}
}
sum+=min;
vis[k]=1;
for(i=1;i<=n;i++)
{
if(!vis[i]&&dis[i]>g[k][i])
dis[i]=g[k][i];
}
}
printf("The highest possible quality is 1/%d.\n",sum);
}
int main(){
while(scanf("%d",&n),n)
{
int i,j;
memset(g,0,sizeof(g));
for(i=1;i<=n;i++)
scanf("%s",b[i]);
for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
g[i][j]=g[j][i]=bj(b[i],b[j]);
}
}
prim();
}
return 0;
}