F - Truck History
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个字符串,求连通他们的最小距离和。
题解:连通距离为两个字符串的不同字符个数,创建一个函数计算距离,最小生成树模板题,注意输出末尾有句号。
AC代码:
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<cmath>
using namespace std;
int n,t,m;
int sum;
const int maxn = 2005;
const int inf = 0x3f3f3f3f;
int a[maxn][maxn];
int vis[maxn];
char x[maxn][8];
int length[maxn];
void prim()
{
for(int i=1;i<=n;i++)
{
vis[i]=0;
length[i]=a[1][i];
}
for(int i=1;i<=n;i++)
{
int u=inf;
int v;
for(int j=1;j<=n;j++)
{
if(vis[j]==0&&length[j]<u)
{
u=length[j];
v=j;
}
}
vis[v]=1;
sum+=length[v];
for(int j=1;j<=n;j++)
{
if(vis[j]==0&&a[v][j]<length[j])
{
length[j]=a[v][j];
}
}
}
}
int leng(char *a1,char *b1)
{
int k=0;
for(int i=0;i<=6;i++)
{
if(a1[i]!=b1[i])k++;
}
return k;
}
int main()
{
while(cin>>n)
{
sum=0;
if(!n)break;
for(int i=1;i<=n;i++)
{
cin>>x[i];
}
for(int i=1;i<=n;i++)
for(int j=i;j<=n;j++)
a[i][j]=a[j][i]=leng(x[i],x[j]);
prim();
cout<<"The highest possible quality is 1/"<<sum<<"."<<endl;
}
}
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<cmath>
using namespace std;
int n,t,m;
int sum;
const int maxn = 2005;
const int inf = 0x3f3f3f3f;
int a[maxn][maxn];
int vis[maxn];
char x[maxn][8];
int length[maxn];
void prim()
{
for(int i=1;i<=n;i++)
{
vis[i]=0;
length[i]=a[1][i];
}
for(int i=1;i<=n;i++)
{
int u=inf;
int v;
for(int j=1;j<=n;j++)
{
if(vis[j]==0&&length[j]<u)
{
u=length[j];
v=j;
}
}
vis[v]=1;
sum+=length[v];
for(int j=1;j<=n;j++)
{
if(vis[j]==0&&a[v][j]<length[j])
{
length[j]=a[v][j];
}
}
}
}
int leng(char *a1,char *b1)
{
int k=0;
for(int i=0;i<=6;i++)
{
if(a1[i]!=b1[i])k++;
}
return k;
}
int main()
{
while(cin>>n)
{
sum=0;
if(!n)break;
for(int i=1;i<=n;i++)
{
cin>>x[i];
}
for(int i=1;i<=n;i++)
for(int j=i;j<=n;j++)
a[i][j]=a[j][i]=leng(x[i],x[j]);
prim();
cout<<"The highest possible quality is 1/"<<sum<<"."<<endl;
}
}
这篇博客介绍了一个字符串连接问题,通过计算不同字符串之间的差异字符数来确定距离,并利用Prim算法找到连接所有字符串的最小距离总和。AC代码中展示了如何实现这一算法并输出结果。
242

被折叠的 条评论
为什么被折叠?



