博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Poj1789--Truck History
阅读量:6929 次
发布时间:2019-06-27

本文共 3863 字,大约阅读时间需要 12 分钟。

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 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

4aaaaaaabaaaaaaabaaaaaaabaaaa0

Sample Output

The highest possible quality is 1/3.

Source

RE: 题目真长
→ → "RT"。 就是说给几个串, 串与串之间有些字母不同, 求任取两串最小不同字母数之和; 可以转化成最小生成树理解, 任取两串, 串的编号做点, 两串不同字母数做权值, 转化成了求最小生成树问题,
1 #include 
2 #include
3 #include
4 using namespace std; 5 int map[2020][2020], dis[2020]; bool vis[2020]; 6 const int INF = 0x3f3f3f3f; 7 char str[2020][8]; 8 int n; 9 int Deal(int a, int b) //求权值;10 {11 int m = 0;12 for(int i = 0; i < 7; i++)13 if(str[a][i] != str[b][i])14 m++;15 return m;16 }17 void Prim()18 {19 int sum = 0;20 memset(vis, true, sizeof(vis));21 for(int i = 1; i <= n; i++)22 dis[i] = map[1][i];23 vis[1] = false;24 for(int i = 1; i < n; i++)25 {26 int temp, min = INF;27 for(int j = 1; j <= n; j++)28 {29 if(vis[j] && dis[j] < min)30 {31 min = dis[j];32 temp = j; 33 }34 }35 if(min == INF)36 return ;37 vis[temp] = false;38 sum += min;39 for(int j = 1; j <= n; j++)40 if(vis[j] && dis[j] > map[temp][j])41 dis[j] = map[temp][j];42 }43 printf("The highest possible quality is 1/%d.\n", sum);44 }45 int main()46 {47 while(~scanf("%d", &n), n)48 {49 for(int i = 1; i <= n; i++)50 scanf("%s", str[i]);51 for(int i = 1; i <= n; i++)52 {53 map[i][i] = 0;54 for(int j = 1; j < i; j++)55 map[i][j] = map[j][i] = Deal(i, j);56 }57 Prim();58 }59 return 0;60 }

 

转载于:https://www.cnblogs.com/soTired/p/4722125.html

你可能感兴趣的文章
iphone iPhone开发应用UIImage图片对象操作
查看>>
javascript中parseInt和Number函数的用法区别
查看>>
字符串json转换为xml xml转换json
查看>>
基于 Spring 和 iBATIS 的动态可更新多数据源持久层
查看>>
会移动的文字
查看>>
SQL XQuery的Action
查看>>
从微软的DBML文件中我们能学到什么(它告诉了我们什么是微软的重中之重)~一 DBContext的构造方法,方法的重载...
查看>>
《项目经理的领导力与沟通》读书笔记
查看>>
剪刀、石头、布 每局必胜法
查看>>
系统日志
查看>>
正则表达式 支持特性
查看>>
ASP.NET : 如何将服务端的多个文件打包下载
查看>>
Web应用Word编辑
查看>>
同期及上期数据对比处理示例.sql
查看>>
Android之Animations的使用
查看>>
iOS地图位置开发
查看>>
Linux 学习_在虚拟机上面安装RedHat 9(图+文)
查看>>
阻塞与死锁(三)——死锁的定位及解决方法
查看>>
当数据库某张表数据发生变化时,更新c#程序中缓存的用法
查看>>
在Windows的CMD中如何设置支持UTF8编码?
查看>>