Poj1789--truck history

Source: Internet
Author: User

Truck History
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 21535 Accepted: 8380

Description

Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks is used for vegetable delivery, and for furniture, or for bricks. The company have its own code describing each type of a truck. The code is simply a string of exactly seven lowercase letters (all letter in each position have a very special meaning bu T is the unimportant for this task). At the beginning of company's history, just a single truck type is used but later other types were derived from it, then From the new types another types were derived, and so on.

Today, ACM is a rich enough to pay historians to study it history. One thing historians tried to find out are 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 is derived from exactly one other truck type (except for the first truck type whic H is not derived from any other type). The quality of a derivation plan was and then defined as
1/σ (TO,TD) d (TO,TD)
Where the sum goes over all pairs of types in the derivation plan such, that's the original type and TD the type derive D from it and D (TO,TD) is the distance of the types.
Since historians failed, you is to the write a program to the 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). Assume that the codes uniquely describe the trucks, i.e., No. Of these N lines is 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 was 1/q.", where 1/q is the quality O f the best derivation plan.

Sample Input

4aaaaaaabaaaaaaabaaaaaaabaaaa0

Sample Output

The highest possible quality is 1/3.

Source

CTU Open 2003RE: The title is really long →→"RT". That is, to give a few strings, strings and strings between some letters, to get two strings of the minimum number of different letters sum; Can be converted to the minimum spanning tree understanding, any two strings, the number of strings to do points, two strings of different letters to do weights, transformed into a minimum spanning tree problem,
1#include <cstdio>2#include <cstring>3#include <iostream>4 using namespacestd;5 intmap[2020][2020], dis[2020];BOOLvis[2020];6 Const intINF =0x3f3f3f3f;7 Charstr[2020][8];8 intN;9 intDeal (intAintb)//To seek the right value;Ten { One     intm =0; A      for(inti =0; I <7; i++) -         if(Str[a][i]! =Str[b][i]) -m++; the     returnm; - } - voidPrim () - { +     intsum =0; -memset (Vis,true,sizeof(Vis)); +      for(inti =1; I <= N; i++) ADis[i] = map[1][i]; atvis[1] =false; -      for(inti =1; I < n; i++) -     { -         inttemp, Min =INF; -          for(intj =1; J <= N; J + +) -         { in             if(Vis[j] && Dis[j] <min) -             { toMin =Dis[j]; +temp =J; -             } the         } *         if(min = =INF) $             return ;Panax NotoginsengVis[temp] =false; -Sum + =min; the          for(intj =1; J <= N; J + +) +             if(Vis[j] && dis[j] >Map[temp][j]) ADIS[J] =Map[temp][j]; the     } +printf"The highest possible quality is 1/%d.\n", sum); - } $ intMain () $ { -      while(~SCANF ("%d", &N), N) -     { the          for(inti =1; I <= N; i++) -scanf"%s", Str[i]);Wuyi          for(inti =1; I <= N; i++) the         { -Map[i][i] =0; Wu              for(intj =1; J < I; J + +) -MAP[I][J] = Map[j][i] =Deal (i, j); About         } $ Prim (); -     } -     return 0; -}

Poj1789--truck history

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.