Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 22865 | Accepted: 8871 |
Description
Input
Output
Sample Input
4 aaaaaaa baaaaaa abaaaaa aabaaaa 0
Sample Output
The highest possible quality is 1/3.
本题大意:求出不同字符串之间的distance(即不同的字母数)利用Prime或者kruskal求解此问题。
注意事项:使用邻接表存边注意将数组开到足够大,本题为4000000.
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
const int INF = 999999999;
const int maxn = 2005;
char str[maxn][10];
struct Edge
{
int from,to,dis,next;
};
Edge e[4000005];
int flag[maxn];
int d[maxn];
int pre[maxn];
int compare(int i,int j)
{
int sum = 0;
for(int k = 0;k < 7;k++)
if(str[i][k] != str[j][k])
sum++;
return sum;
}
int main()
{
int n,i,j;
while(scanf("%d",&n)!=EOF && n)
{
memset(pre,-1,sizeof(pre));
for(i = 0;i < n;i++)
scanf("%s",str[i]);
int m = 0;
for(i = 0;i < n-1;i++)
for(j = i+1 ; j < n; j++)
{
e[m].from = i;
e[m].to = j;
e[m].dis = compare(i,j);
e[m].next = pre[i];
pre[i] = m;
m++;
e[m].from = j;
e[m].to = i;
e[m].dis = compare(i,j);
e[m].next = pre[j];
pre[j] = m;
m++;
}
memset(flag,0,sizeof(flag));
flag[e[pre[0]].from] = 1;
for(i = 0;i < n;i++)
d[i] = INF;
for(i = pre[0];i != -1;i = e[i].next)
d[e[i].to] = e[i].dis;
int sum = 0;
for(i = 1 ; i < n; i++)
{
int x,minn = INF;
for(j = 0 ; j < n; j++)
if(!flag[j] && d[j] <= minn)
minn = d[x = j];
flag[x] = 1;
sum += d[x];
for(j = pre[x] ; j != -1; j = e[j].next)
{
if(!flag[e[j].to] && d[e[j].to] > e[j].dis)
d[e[j].to] = e[j].dis;
}
}
printf("The highest possible quality is 1/%d.\n",sum);
}
return 0;
}