1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
#include<iostream>
#include<cstring>
using
namespace std;
char
a[2222][10];
int dist[2222];
int s[2222];
int n;
int e( int u, int v)
{
int
l=0;
for ( int
i=0;i<7;i++)
if (a[u][i]!=a[v][i])
l++;
return
l;
}
void
Kruskal()
{
int
sum=0;
int
i,j;
dist[1]=0;
memset (s,0, sizeof (s));
for (i=2;i<=n;i++)
{
int
l=0;
for (j=0;j<7;j++)
if (a[1][j]!=a[i][j])
l++;
dist[i]=l;
}
s[1]=1;
for (i=1;i<=n;i++)
{
int
v=-1;
int
min=999999999;
for (j=2;j<=n;j++)
{
if (dist[j]<min&&!s[j])
min=dist[j],v=j;
}
if (v==-1) break ;
sum+=dist[v];
s[v]=1;dist[v]=0;
for (j=2;j<=n;j++)
{
if (dist[j]>e(v,j))
dist[j]=e(v,j);
}
}
printf ( "The highest possible quality is 1/%d.\n" ,sum);
}
int
main()
{
int
i;
while ( scanf ( "%d" ,&n)!=EOF)
{
if (n==0) break ;
for (i=1;i<=n;i++)
scanf ( "%s" ,a[i]);
Kruskal();
}
return
0;
}
|