zoukankan      html  css  js  c++  java
  • POJ 1789

    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.
    题目的难点在于题意不好理解,,还有就是 树很难搭建
    题目大意:就是 对于输入的字符串,,已行尾单位,比如说第一行和第二行在相同位置有1个字符不同那么 起点为1,终点为2,权值为1 即权值就是不同字符的个数

    //编写程序输出最高质量的火车推导计划。火车推倒计划质量的求解公式中的分子为1,分母为t0,td的距离总和。
    //t0,td的距离就是两个输入字符串的字母不同位置的个数。想要推导计划的质量最高就是使分母最小。即t0,td的距离总和最小。
    //实质上是以任意两个字符串为结点,以字符串字母不同位置的个数为两节点之间边的权值,求解最小生成树。
    //因此输入和结点联系密切,使用kruskal算法求解比较方便。
    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    using namespace std;
    const int N=1e5+7;
    const int M=7e7+7;
    char s[N][10];
    struct stu{
    int a,b,c;
    }p[M];

    int pre[N];

    bool cmp(stu x,stu y){
    return x.c<y.c;
    }

    int find(int x){
    int r=x;
    while(r!=pre[r])
    r=pre[r];
    int k=x,j;
    while(k!=r){
    j=pre[k];
    pre[k]=r;
    k=j;
    }
    return r;
    }
    int main(){
    int n;
    while(~scanf("%d",&n)&&n){
    for(int i=0;i<=n;i++){
    pre[i]=i;
    }
    for(int i=0;i<n;i++)
    scanf("%s",s[i]);
    int pos=0;
    //重点 树的搭建
    for(int i=0; i<n; i++)//第i行
    for(int j=n-1; j>i; j--)//从第n-1行到第i行寻找不同的字符个数 ,并用结构体保存
    {
    int sum=0;
    for(int x=0; x<7; x++)
    if(s[i][x]!=s[j][x])
    sum++;
    p[pos].a=i;
    p[pos].b=j;
    p[pos].c=sum;
    pos++;
    }

    sort(p,p+pos,cmp);

    int ans=0;
    for(int i=0;i<pos;i++){
    int fx=find(p[i].a);
    int fy=find(p[i].b);
    if(fx!=fy){
    pre[fx]=fy;
    ans+=p[i].c;
    }
    }

    printf("The highest possible quality is 1/%d. ",ans);
    }
    return 0;
    }

  • 相关阅读:
    设计权限管理系统(十一)
    设计权限管理系统(十)
    设计权限管理系统(九)
    设计权限管理系统(八)
    设计权限管理系统(七)
    设计权限管理系统(六)
    设计权限管理系统(五)
    设计权限管理系统(四)
    设计权限管理系统(三)
    Dom的深度优先遍历和广度优先遍历
  • 原文地址:https://www.cnblogs.com/Accepting/p/11311370.html
Copyright © 2011-2022 走看看