首页 > 其他分享 >Truck History

Truck History

时间:2023-04-20 18:06:49浏览次数:33  
标签:int quality derived truck Truck type types History


Truck History


Time Limit: 2000MS

 

Memory Limit: 65536K

Total Submissions: 21508

 

Accepted: 8361


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


4 aaaaaaa baaaaaa abaaaaa aabaaaa 0


Sample Output


The highest possible quality is 1/3.


//克鲁斯卡尔算法


/*
题意::
		给你n个车,每个车用7个字母表示,他们的差距可以用它们不同字母的个数表示,现在,要先找出它们
		差距和的最小值。然后它们的最小和的倒数即为所求的最大值。 
*/
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int a[2100];
char b[2100][20];//用来存储字符串 
int sum;
struct zz
{
	int f1,f2,d;
}s[4000010];
int cmp(zz x,zz y)//按差距从小到大排序 
{
	return x.d<y.d;
}
int find(int x)
{
	while(x!=a[x])
		x=a[x];
	return x;
}
int marge(int x,int y)
{
	int fx,fy;
	fx=find(x);
	fy=find(y);
	if(fx!=fy)
	{
		a[fx]=fy;
		return 1;
	}
	return 0;
}
int bj(char s1[],char s2[])//比较两个字符串不同字母的个数 
{
	int cnt=0,i;
	for(i=0;i<7;i++)
	{
		if(s1[i]!=s2[i])
			cnt++;
	}
	return cnt;
}
int main(){
	int t,i,j,l,k,n;
	while(scanf("%d",&t),t)
	{
		for(i=1;i<=t;i++)
			a[i]=i;
		for(i=1;i<=t;i++)
			scanf("%s",b[i]);		
		k=0;
		for(i=1;i<t;i++)  //重点:记录两个串不同字母的个数 
		for(j=1+i;j<=t;j++)
		{
			s[k].f1=i;s[k].f2=j;
			s[k++].d=bj(b[i],b[j]);
		}
		sort(s,s+k,cmp);
		sum=0;n=0;
		for(i=0;i<k;i++)
		{
			if(marge(s[i].f1,s[i].f2))
			{
				sum+=s[i].d;
				n++;//重点:用来判定是否已经成树, 
			}
			if(n==t-1)//树的定义,n个点,n-1条边,如果已经完成,后面的即无需在加,节省时间,WA了两次 
				break;
		}
		if(n==t-1)
			printf("The highest possible quality is 1/%d.\n",sum);
	}
	return 0;
}
<pre class="cpp" name="code">//prim算法 
#include<stdio.h>
#include<string.h>
#define mx 0x3f3f3f3f
char b[2100][20];
int n;
int g[2010][2010],vis[2100],dis[2100];
int bj(char s1[],char s2[])
{
	int i,cnt=0;
	for(i=0;i<7;i++)
	{
		if(s1[i]!=s2[i])
		{
			cnt++;
		}
	}
	return cnt;
}
void prim()
{
	int i,j,k,min,sum=0;
	memset(vis,0,sizeof(vis));
	for(i=1;i<=n;i++)
	{
		dis[i]=g[1][i];
	}
	dis[1]=0;
	vis[1]=1;
	for(j=1;j<n;j++)
	{
		min=mx;k=1;
		for(i=1;i<=n;i++)
		{
			if(!vis[i]&&dis[i]<min)
			{
				min=dis[i];
				k=i;
			}
		}
		sum+=min;
		vis[k]=1;
		for(i=1;i<=n;i++)
		{
			if(!vis[i]&&dis[i]>g[k][i])
			dis[i]=g[k][i];
		}
	}
	printf("The highest possible quality is 1/%d.\n",sum);
}
int main(){
	while(scanf("%d",&n),n)
	{
		int i,j;
		memset(g,0,sizeof(g));
		for(i=1;i<=n;i++)
			scanf("%s",b[i]);
		for(i=1;i<=n;i++)
		{
			for(j=i+1;j<=n;j++)
			{
				g[i][j]=g[j][i]=bj(b[i],b[j]);
			}
		}
		prim();
	}
	return 0;
}




标签:int,quality,derived,truck,Truck,type,types,History
From: https://blog.51cto.com/u_16079508/6210093

相关文章

  • Robotruck UVA - 1169
    有n个垃圾,第i个垃圾的坐标为(xi,yi),重量为wi。有一个机器人,要按照编号从小到大的顺序捡起所有垃圾并扔进垃圾桶(垃圾桶在原点(0,0))。机器人可以捡起几个垃圾以后一起扔掉,但任何时候其手中的垃圾总重量不能超过最大载重C。两点间的行走距离为曼哈顿距离(即横坐标之差的绝对值加上纵......
  • popState api详解 history.pushState 案例
    popStateAPI是HTML5引入的一种浏览器历史记录相关的API。它提供了一种监听浏览器历史栈发生变化的途径,当用户通过前进/后退按钮或者其他方式改变了当前的历史状态时,该API就会触发相应的事件,开发者可以在事件处理函数中通过获取到的历史状态数据做出相应的响应。在使用popStateA......
  • Vue.js history模式与hash模式
    视频13.路由器的两种工作模式对于一个url来说,什么是hash值?——#及其后面的内容就是hash值。hash值不会包含在HTTP请求中,即:hash值不会带给服务器。hash模式:地址中永远带着#号,不美观。若以后将地址通过第三方手机app分享,若app校验严格,则地址会被标记为不合法。兼容性较......
  • 【CentOS7】history命令
    修改~/.bashrc,增加历史行数HISTSIZE=5000HISTCONTROL=ignorespaceignoredups   默认,忽略重复命令ignorespace   忽略所有以空格开头的命令ignoreboth   ignoredups和ignorespace的组合erasedups   删除历史记录中重复命令,相同的指令仅保留最近的一个......
  • WABCO DIAGNOSTIC KIT (WDI) WABCO Trailer and Truck Diagnostic Interface
    WabcoDiagnosticKit isauniversaldiagnostictooldesignedformaintenanceoftrailers,trucksandbuses.WabcoDiagnostictoolsupportsmostpopularandw......
  • vscode的Git History,GitLens — Git supercharged插件
    Gitsupercharged插件GitSupercharged插件是一个可以扩展和增强您的Git操作的VisualStudioCode插件。它提供了一系列实用工具,使得在VSCode中管理和使用Git变得更加高效......
  • 详解一张历史数据库性能的关键表--dba_hist_active_sess_history
    在很多情况下,当数据库发生性能问题的时候,我们并没有机会来收集足够的诊断信息,比如systemstatedump或者hanganalyze,甚至问题发生的时候DBA根本不在场。这给我们诊断问题......
  • history命令的优化
    1.history添加日期1.编辑文件vim/etc/bashrc2.末尾添加:HISTTIMEFORMAT="%F%T"exportHISTTIMEFORMAT3.刷新source/etc/bashrc4.运行history查看history2.汇总命令,并且......
  • dba_hist_active_sess_history和v$active_session_history
    oracle视图学习-dba_hist_active_sess_history和v$active_session_history 分类: Oracle2018-05-0608:02:34 博客文章除注明转载外,均为原创。转载请注明出处。本......
  • vscode的Git History,GitLens — Git supercharged插件
    Gitsupercharged插件GitSupercharged插件是一个可以扩展和增强您的Git操作的VisualStudioCode插件。它提供了一系列实用工具,使得在VSCode中管理和使用Git变得更加高......