sicily1006_heidi 1006-程序员宅基地

技术标签: sicily  

Constraints

Time Limit: 1 secs, Memory Limit: 32 MB

Description

It's preseason and the local newspaper wants to publish a preseason ranking of the teams in the local amateur basketball league. The teams are the Ants, the Buckets, the Cats, the Dribblers, and the Elephants. When Scoop McGee, sports editor of the paper, gets the rankings from the selected local experts down at the hardware store, he's dismayed to find that there doesn't appear to be total agreement and so he's wondering what ranking to publish that would most accurately reflect the rankings he got from the experts. He’s found that finding the median ranking from among all possible rankings is one way to go. 

The median ranking is computed as follows: Given any two rankings, for instance ACDBE and ABCDE, the distance between the two rankings is defined as the total number of pairs of teams that are given different relative orderings. In our example, the pair B, C is given a different ordering by the two rankings. (The first ranking has C above B while the second ranking has the opposite.) The only other pair that the two rankings disagree on is B, D; thus, the distance between these two rankings is 2. The median ranking of a set of rankings is that ranking whose sum of distances to all the given rankings is minimal. (Note we could have more than one median ranking.) The median ranking may or may not be one of the given rankings. 

Suppose there are 4 voters that have given the rankings: ABDCE, BACDE, ABCED and ACBDE. Consider two candidate median rankings ABCDE and CDEAB. The sum of distances from the ranking ABCDE to the four voted rankings is 1 + 1 + 1 + 1 = 4. We'll call this sum the value of the ranking ABCDE. The value of the ranking CDEAB is 7 + 7 + 7 + 5 = 26. 

It turns out that ABCDE is in fact the median ranking with a value of 4. 

Input

There will be multiple input sets. Input for each set is a positive integer n on a line by itself, followed by n lines (n no more than 100), each containing a permutation of the letters A, B, C, D and E, left-justified with no spaces. The final input set is followed by a line containing a 0, indicating end of input.

Output

Output for each input set should be one line of the form: 

ranking is the median ranking with value value. 

Of course ranking should be replaced by the correct ranking and value with the correct value. If there is more than one median ranking, you should output the one which comes first alphabetically. 

Sample Input

4
ABDCE
BACDE
ABCED
ACBDE
0

Sample Output

ABCDE is the median ranking with value 4.



题目简述:

这道题真的是很长……但是核心内容只有一点:求字符串“ABCDE”的全排列中与给定字符串的差异最小的那个字符串。

重点是  1、如何求“ABCDE”的全排列  2、如何求与所给字符串的差异

算法分析:

递归、回溯、深度优先搜索

数据结构:

结构体

解题思路:

1、求数字{1,2,3,4,5}全排列方法:



#include<cstdio>
#include<iostream>
using namespace std;
int a[1000],v[1000],n;
void print(){
  for (int i=1;i<=n;i++) printf("%d ",a[i]); //将每位输出 
  puts(""); //换行 
}
 
void DFS(int dep){
  if (dep==n) print(); //如果搜到一个结果输出 
  dep++; //查找当前要处理位 
  for (int i=1;i<=n;i++) { //枚举当前位 
   if (v[i]) continue; //如果这个数之前被选过就跳过 
   v[i]=1; //选中当前位 
   a[dep]=i;//将当前位存入数组 
   DFS(dep);//搜索下一位 
   v[i]=0;//取消选中当前位 
  }
}
 
int main(){
  scanf("%d",&n); //读入
  DFS(0); //深搜
}


2、求字符{A,B,C,D,E}的全排列的方法:

借鉴上面代码并改动一下就可以了,char类型和int类型其实是对应的 : ‘B’ - 'A' = (int)1;

注:在网上看到很多人写用使用STL<algorithm>的next_permutation函数生成全排列(C++),当然可以啦

#include <algorithm>
bool next_permutation( iterator start, iterator end );

The next_permutation() function attempts to transform the given range of elements [start,end) into the next lexicographically greater permutation of elements. If it succeeds, it returns true, otherwise, it returns false.


3、求两个字符相异程度方法:

例如:ABCDE 和 BACDE

我先给两个字符串里面的字符编号:

1 2  3  4  5  

A B C D E  

1  2  3 4 5

B A C D E

然后给字符串里的字符按字典序排序,原来的编号不变:

1 2  3  4  5   

A B C D E 

2 1  3  4  5  

A B C D E

之后,我发现,再第一个字符串中编号A<B而在第二个字符串中对应字符的编号A>B,这就表明这是一个差异

其余的循环一一对比,总共需要 4+3+2+1=10次比较。

<span style="font-size:14px;">void search() {
	for (int k = 1; k <= n; k++)
		for (int i = 1; i <= DEEP; i++)
			for (int j = i + 1; j <= DEEP; j++)
				if ((a[0][i].num < a[0][j].num)
						^ (ran[k][i].num < ran[k][j].num))
					sum++;
}</span>



代码:

#include <cstdio>
#include <iostream>
#include <stdio.h>

#define DEEP 5

using namespace std;

int v[102], n;
int sum, m;
struct list {
	char c;
	int num;
};
list ran[101][6]; //储存输入的所有字符串
list a[1][6];     //全排列中的当前排列
list best[1][6];  //最佳结果

//将字符按字典序排序
void sorting(list *p) {
	for (int i = 1; i <= DEEP; i++)
		for (int j = i + 1; j <= DEEP; j++)
			if (p[i].c > p[j].c) {
				list hold = p[i];
				p[i] = p[j];
				p[j] = hold;
			}
}

//查找相异
void search() {
	for (int k = 1; k <= n; k++)
		for (int i = 1; i <= DEEP; i++)
			for (int j = i + 1; j <= DEEP; j++)
				if ((a[0][i].num < a[0][j].num)
						^ (ran[k][i].num < ran[k][j].num))
					sum++;
}

//生成全排列
void DFS(int dep) {
	if (dep == DEEP) {
		list hold[1][6];
		for (int i = 1; i <= DEEP; i++) {
			hold[0][i] = a[0][i];
		}
		sorting(a[0]);
		sum = 0;
		search(); //如果搜到一个结果输出
		if (sum < m) {
			for (int i = 1; i <= DEEP; i++) {
				best[0][i] = hold[0][i];
			}
			m = sum;
		}
		//还原现场
		for (int i = 1; i <= DEEP; i++) {
			a[0][i] = hold[0][i];
		}
	}
	dep++; //查找当前要处理位
	for (int i = 1; i <= DEEP; i++) { //枚举当前位
		if (v[i])
			continue; //如果这个数之前被选过就跳过
		v[i] = 1; //选中当前位
		a[0][dep].c = i + 'A' - 1; //将当前位存入数组
		a[0][dep].num = dep;
		DFS(dep); //搜索下一位
		v[i] = 0; //取消选中当前位
	}
}

int main() {
	cin >> n;
	m = 10000000;
	while (n != 0) {
		m = 1000000;
		getchar();
		for (int i = 1; i <= n; i++) {
			for (int j = 1; j <= DEEP; j++) {
				ran[i][j].c = getchar();
				ran[i][j].num = j;
			}
			getchar();
			sorting(ran[i]);
		}

		DFS(0); //深搜
		for (int i = 1; i <= DEEP; i++) {
			cout << best[0][i].c;
		}
		cout << " is the median ranking with value " << m << "." << endl;
		cin >> n;
	}
	return 0;
}


版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/heidi_xxy/article/details/39401523

智能推荐

sql server 2000 示例数据库 Pubs 全库脚本 SQLServer2000 自带数据库-程序员宅基地

文章浏览阅读232次。/* *//* InstPubs.SQL - Creates the Pubs database */ /* *//*** Copyright Microsoft, Inc. 1994 - 2000** All Rights Reserved.*/SET NOCOUNT ONGOset nocount onset dateformat mdyUSE masterdeclare @d..._sql2000中自带的pubs数据库中的表

【无标题】App iOS端适配iOS 15系统_lsapplicationqueriesschemes 超过 50 怎么办-程序员宅基地

文章浏览阅读2.6k次。各位好:App iOS端适配iOS 15系统,适配后将使用新的xcode 13打包提交App Store。一、适配内容:1、新增了iPhone 13 mini机型(尺寸同iPhone12 mini),5.4 英寸 (对角线) OLED 全面屏,屏幕分辨率为2340 x 1080 像素。如果是通过分辨率来判断则需要增加一个模式。 #define iPhone13mini ([UIScreen instancesRespondToSelector:@selector(currentMo_lsapplicationqueriesschemes 超过 50 怎么办

抓包工具Fiddler的下载安装使用_fiddler抓包下载-程序员宅基地

文章浏览阅读497次。右侧显示就是我们主机发送http/https请求的记录。如果我们要查看某一次访问,可以双击该记录,在右侧就会显示这次http请求的内容以及返回的响应的内容。右键全选,点击remove,选择selected sessions,就能删除选择的sessions。安装过程只用一路next即可;_fiddler抓包下载

html语言ppt,htmlppt课件-程序员宅基地

文章浏览阅读642次。PPT内容这是htmlppt课件,关于第2章Web编程技术,包括了HTML的发展历史,HTML的基本框架,HTML的各种常用标记:文字标记、图片标记、超级链接标记,CSS的基本使用方法,如何让CSS与HTML协同工作,JavaScript中的变量、数组、表达式、运算符、流程控制语句,JavaScript的函数、内置对象、浏览器对象的层次和DOM模型的建立和使用等内容,欢迎点击下载。第2章 Web编..._html if elseppt课件

solr html显示,Solr查询界面-程序员宅基地

文章浏览阅读259次。您可以使用查询界面将搜索查询提交给 Solr 集合并分析结果。在下面截图中的例子中,查询已经被提交,并且界面显示了作为 JSON 形式发送到浏览器的查询结果。在这个例子中,genre:Fantasy 的查询被发送到 “films” 集合。表单中的所有其他选项都使用了默认值,下表中对此进行了简要介绍,本指南的后面部分将对此进行详细介绍。该响应显示在窗体的右侧。对 Solr 的请求只是简单的 HTTP..._solr查询界面

【Java EE】Spring请求如何传递参数详解-程序员宅基地

文章浏览阅读1.5k次,点赞65次,收藏51次。Spring请求如何传递参数详解,传递单个参数,传递多个参数,传递对象,后端参数重命名(后端参数映射),传递数组,传递集合,传递JSON数据,获取URL中参数@PathVariable,上传文件@RequestPart,获取Cooki/Session,获取Header

随便推点

RuntimeError: split_size can only be 0 if dimension size is 0, but got dimension size of 2-程序员宅基地

文章浏览阅读624次。使用pytorch时遇到下面的问题RuntimeError: split_size can only be 0 if dimension size is 0, but got dimension size of 2原因:训练的batch size 比使用的GPU数量少,导致上述问题。解决办法增加batch size数值,保证为GPU数量整数倍。参考:1.https://discuss.pytorch.org/t/concatenating-images/40961/10_split_size can only be 0 if dimension size is 0, but got dimension size of 1

RabbitMQ订阅发布的消息,通过WebSocket实现数据实时推送到前端_rabbitmq怎么返回给前端数据-程序员宅基地

文章浏览阅读7.3k次,点赞3次,收藏12次。一、架构简单概述 RabbitMQ消息队列服务善于解决多系统、异构系统间的数据交换(消息通知/通讯)问题,并且可以订阅和发布,而随着HTML5诞生的WebSocket协议实现了浏览器与服务器的全双工通信,扩展了浏览器与服务端的通信功能,使服务端也能主动向客户端发送数据。 因此,我们可以使用RabbitMQ的订阅发布技术,订阅后,当RabbitMQ端有新的数据就直接发布到指定的queue,订_rabbitmq怎么返回给前端数据

Mendix Excel导出介绍_mendix实现excel导出-程序员宅基地

文章浏览阅读320次。本文介绍了Excel导出的两种方式及成果展示_mendix实现excel导出

5 gtm 工作原理_基于GTM法的水泥稳定碎石力学性能研究-程序员宅基地

文章浏览阅读226次。文章来源:微信公众号”沥青路面“引 言众所周知,以水泥稳定碎石为代表的半刚性材料是中国目前使用最为广泛的基层材料,因为其力学性能优良、使用成本较低、原材料来源广泛和施工工艺简单等优点,水泥稳定碎石在未来十几年内仍将是中国使用最为广泛的基层材料。目前水泥稳定碎石在设计和施工方面存在一些问题,例如室内成型方式与实际道路受力状态存在一定差异;设计指标和施工检测指标相关性不足;对矿质石料级配的要求没有体现..._无侧限抗压强度与劈裂强度的的关系

黑科技,Python 脚本帮你找出微信上删除你好友的人_微信出现brandsessionholder-程序员宅基地

文章浏览阅读1.5k次。编者按:本文来自稀土掘金江昪编译自 Github:0x5e/wechat-deleted-friends “ 清理下[微笑],不用回。你的朋友圈没事也该清清了,打开设置,通用,功能,群助手,全选,把我的信息粘贴一下,就可以了,发送就知道谁把你删了,方便你清人,不清不知道 ,一清吓一跳。” 相信大家在微信上一定被上面的这段话刷过屏,群发消息应该算是微信上流传最广的找到删除好友的方法..._微信出现brandsessionholder

MySQL存储过程 游标循环的使用_存储过程 重复定义同名游标 会覆盖吗?-程序员宅基地

文章浏览阅读1.5k次。MySQL存储过程 游标循环的使用_存储过程 重复定义同名游标 会覆盖吗?

推荐文章

热门文章

相关标签