UVa 340 Master-Mind Hints (优化查找&复制数组)

FreeRTOS:卡在configASSERT( ( pxQueue ) )问题的解决 FreeRTOS卡在configASSERT( ( pxQueue ) )问题的解决 阅读详情

340 - Master-Mind Hints

Time limit: 3.000 seconds

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=98&page=show_problem&problem=276

MasterMind is a game for two players. One of them, Designer, selects a secret code. The other, Breaker, tries to break it. A code is no more than a row of colored dots. At the beginning of a game, the players agree upon the length N that a code must have and upon the colors that may occur in a code.

In order to break the code, Breaker makes a number of guesses, each guess itself being a code. After each guess Designer gives a hint, stating to what extent the guess matches his secret code.

In this problem you will be given a secret code tex2html_wrap_inline35 and a guess tex2html_wrap_inline37 , and are to determine the hint. A hint consists of a pair of numbers determined as follows.

A match is a pair (i,j), tex2html_wrap_inline41 and tex2html_wrap_inline43 , such that tex2html_wrap_inline45 . Match (i,j) is called strong when i =j, and is called weak otherwise. Two matches (i,j) and (p,q) are called independent when i = p if and only if j = q. A set of matches is called independent when all of its members are pairwise independent.

Designer chooses an independent set M of matches for which the total number of matches and the number of strong matches are both maximal. The hint then consists of the number of strong followed by the number of weak matches in M. Note that these numbers are uniquely determined by the secret code and the guess. If the hint turns out to be (n,0), then the guess is identical to the secret code.

Input

The input will consist of data for a number of games. The input for each game begins with an integer specifying N (the length of the code). Following these will be the secret code, represented as N integers, which we will limit to the range 1 to 9. There will then follow an arbitrary number of guesses, each also represented as N integers, each in the range 1 to 9. Following the last guess in each game will be N zeroes; these zeroes are not to be considered as a guess.

Following the data for the first game will appear data for the second game (if any) beginning with a new value for N. The last game in the input will be followed by a single zero (when a value for N would normally be specified). The maximum value for N will be 1000.

Output

The output for each game should list the hints that would be generated for each guess, in order, one hint per line. Each hint should be represented as a pair of integers enclosed in parentheses and separated by a comma. The entire list of hints for each game should be prefixed by a heading indicating the game number; games are numbered sequentially starting with 1. Look at the samples below for the exact format.

Sample Input

4
1 3 5 5
1 1 2 3
4 3 3 5
6 5 5 1
6 1 3 5
1 3 5 5
0 0 0 0
10
1 2 2 2 4 5 6 6 6 9
1 2 3 4 5 6 7 8 9 1
1 1 2 2 3 3 4 4 5 5
1 2 1 3 1 5 1 6 1 9
1 2 2 5 5 5 6 6 6 7
0 0 0 0 0 0 0 0 0 0
0

Sample Output

Game 1:
    (1,1)
    (2,0)
    (1,2)
    (1,2)
    (4,0)
Game 2:
    (2,4)
    (3,2)
    (5,0)
    (7,0)

学英语:

1. Match (i,j) is called strong when i =j, and is called weak otherwise.

当i=j时,匹配(i,j)被叫做强匹配,反之叫做弱匹配。

2. Two matches (i,j) and (p,q) are called independent when i = p if and only if j = q. A set of matches is called independent when all of its members are pairwise independent.

当i=p时,两个匹配(i,j)和(p,q)被叫做无关的,当且仅当j=q。一个匹配集被称作无关的,当它的所有元素对都是无关的。

3. Designer chooses an independent set M of matches for which the total number of matches and the number of strong matches are both maximal.

设计者选择一个匹配集M,它的总匹配数和强匹配数都是最大的。

4. The hint then consists of the number of strong followed by the number of weak matches in M.

暗示包含强匹配的数目,后面跟着一个弱匹配的数目。


注意:

1. 上面第二句话说明像(5,7)和(5,8)这样不是无关的匹配,不能同时存在于一个匹配集中。

2. 上面第三句话说明计算暗示时,优先计算强匹配的数目。


思路:由于只有9个数字,所以只需要统计每个数字出现的个数即可。

强匹配的数目:相同列相同数字的统计一遍,然后相同列相同数字对应的个数减一。

弱匹配的数目:统计1~9在两序列中出现的个数,每个数字加其再两个序列中出现个数的较小值,详见代码。


完整代码:

/*0.019s*/

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

int s[1005], g[1005];
int a[15], b[15], c[15];

int main(void)
{
	int cas = 0, n;
	while (scanf("%d", &n), n)
	{
		memset(c, 0, sizeof(c));
		for (int i = 0; i < n; i++)
		{
			scanf("%d", &s[i]);
			c[s[i]]++;
		}
		printf("Game %d:\n", ++cas);
		while (true)
		{
			memcpy(a, c, sizeof(c));// 技巧:用memcpy将c[]中统计的1-9的数的个数赋给a[]
			memset(b, 0, sizeof(b));
			for (int i = 0; i < n; i++)
			{
				scanf("%d", &g[i]);
				b[g[i]]++;
			}
			if (!g[0]) break;
			int x = 0, y = 0;
			for (int i = 0; i < n; i++)
				if (s[i] == g[i])
				{
					a[s[i]]--;
					b[s[i]]--;
					x++;
				}
			for (int i = 1; i <= 9; i++)
				y += min(a[i], b[i]);
			printf("    (%d,%d)\n", x, y);
		}
	}
	return 0;
}

【漏洞复现】爱数云盘 Usrm_GetAllUsers 未授权访问漏洞 爱数 AnyShare存在未授权漏洞,未授权的攻击者可以通过该漏洞获取用户名信息。 阅读详情

相关推荐

微腔光频梳仿真入门:MATLAB求解LLE方程全流程解析

光学频率梳在精密测量、光通信和微波光子学中有着广阔应用,而微环谐振腔中的Kerr非线性与四波混频效应能够紧凑地产生宽带梳齿。仿真这类系统的核心是求解Lugiato-Lefever方程(LLE),但直接从物理参数到数值代码往往让初学者无从下手。本文从微腔光频梳的物理图像出发,介绍LLE方程的推导与归一化方法,并重点讲解MATLAB数值实现的两种思路:基于ode45的频域耦合方程组求解与分裂步长RK4IP方法。同时讨论失谐扫描、孤子态识别、参数稳定性边界,以及如何将真实器件参数换算为无量纲仿真参数。无论你是刚接

weixin_32325225的博客 293

UVa 340 - Master-Mind Hints

Master-Mind Hints  MasterMind is a game for two players. One of them, Designer, selects asecret code. The other,Breaker, tries to break it. A code is no morethan a row of colored dots. At t

ACM2272902662的专栏 994

如何在高版本的ubuntu系统里安装gcc4.1.2

1、下载安装包,一共五个http://archive.ubuntu.com/ubuntu/pool/universe/g/gcc-4.1/,根据计算机系统位数   进行下载,64位下载amd64结尾的。开头分别是cpp-4.1_4.1.2、g++-4.1_4.1.2、gcc-4.1-base_4.1.2、gcc-     4.1_4.1.2、libstdc++6-4.1 2、使用sudo d

lsr_fighting的博客 1931

算典03_例题_04_Uva-340

猜数字游戏 数字对且位置也对的个数--直接对比答案和猜的数组即可 数字对但是位置不对的个数 这个时候需要遍历i(1~9),每次遍历,都寻找两个数组里等于i的元素个数,将得到的较小的那个元素个数加入结果中去,遍历完得到的结果便是只考虑数字而不考虑位置的个数,用它减去之前数字对且位置也对的个数,即为数字对但位置不对的个数

duaduac.com 414

保姆级教程:用SSH手动重建黑群晖RAID1阵列,修复‘存储空间损毁’

本文提供了一份详细的SSH手动重建黑群晖RAID1阵列的教程,帮助用户修复‘存储空间损毁’问题。通过深入解析mdadm软RAID的底层逻辑,从诊断到重建的全过程,确保用户能够掌握黑群晖存储池的修复技术。适用于技术爱好者和系统管理员。

weixin_30319153的博客 293

ComfyUI-controlnet控制出图

提供加载预处理器节点。

Bu2n的博客 5496

4.猜数字游戏的提示MasterMind

MasterMind is a game for two players. One of them, Designer, selects a secret code. The other, Breaker, tries to break it. A code is no more than a row of colored dots. At the beginning of a game, the

小女不才 1062

Keil调试技巧:3种方法查看Cortex-M4的SCB寄存器(含CPUID内存映射解析)

本文详细介绍了在Keil MDK中查看Cortex-M4内核SCB寄存器的三种实战方法:使用Watch窗口进行符号化观察、通过Memory窗口直接查看物理内存映射,以及利用SFR寄存器浏览器解析位域信息。文章重点解析了CPUID寄存器的内存映射地址(0xE000ED00)及其位域含义,帮助开发者深入理解处理器身份识别与底层调试技巧,提升嵌入式系统调试效率。

h6i7j8的博客 571

例题3-4 猜数字游戏的提示 UVa340

算法竞赛入门经典(第2版)第3章 数组和字符串 例题3-4 猜数字游戏的提示 UVa340 感悟。 1、自认为这个程序写得还可以。 2、开始阶段一团浆糊,只能从数据的输入输出入手,先整理好输入输出格式。 3、进行输入输出测试阶段,输入太繁琐,果断改成采用文件的方式(FILE *fin)输入数据,测试成功,提交时,再改成标准输入。 4、程序是每当输出结果不符要求,调试,输出结果还不符

mrcrack的博客 2695

别让你的URDF模型在Gazebo里‘躺平’:手把手教你配置libgazebo_ros_diff_drive插件让两轮小车动起来

本文详细解析了如何在Gazebo中配置libgazebo_ros_diff_drive插件,使两轮小车模型实现动态仿真。从URDF模型到传动装置,再到差速驱动插件的精细调参,手把手教你解决机器人‘躺平’问题,提升ROS1与Gazebo仿真的实战能力。

weixin_42552716的博客 334

TCGA数据下载保姆级教程:用GDC-client从零搞定,附配置文件避坑指南

本文提供TCGA数据下载的完整教程,详细讲解如何使用GDC-client工具从零开始配置到高效获取基因组数据。涵盖环境准备、认证配置、数据检索、高效下载及故障处理等关键步骤,并附配置文件避坑指南,帮助生物医学研究者快速掌握TCGA数据下载技巧。

weixin_30892037的博客 262

uva340 - Master-Mind Hints

MasterMind is a game for two players. One of them, Designer, selects a secret code. The other,Breaker, tries to break it. A code is no more than a row of colored dots. At the beginning of a game, the

uva code 2737

YOLOv8【第十一章:视频追踪与流处理篇·第6节】BoT-SORT 改进:结合相机运动补偿(CMC)提升追踪稳定性!

🏆 本文收录于 《YOLOv8实战:从入门到深度优化》 专栏。该专栏系统复现并梳理全网各类 YOLOv8 改进与实战案例(当前已覆盖分类 / 检测 / 分割 / 追踪 / 关键点 / OBB 检测等方向),坚持持续更新 + 深度解析,质量分长期稳定在 97 分以上,可视为当前市面上 覆盖较全、更新较快、实战导向极强 的 YOLO 改进系列内容之一。

**My Coding Family** 718

排序检索A - Master-Mind Hints

MasterMind is a game for two players. One of them, Designer, selects a secret code. The other, Breaker, tries to break it. A code is no more than a row of colored dots. At the beginning of a game, th

katydid3018的博客 338

迈瑞BS系列全自动生化分析仪LIS协议接口手册(内含HL7和ASTM)

迈瑞BS系列全自动生化分析仪LIS协议接口手册,内含HL7和ASTM)

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

UVA340-Master-Mind Hints(紫书例题3.4)

MasterMind is a game for two players. One of them, Designer, selects a secret code. The other, Breaker, tries to break it. A code is no more than a row of colored dots. At the beginning of a game, the...

独钓门前月 563

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

Mastermind游戏

Mastermind Game

wuyongpeng的专栏 6688

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an &ldquo;advanced algorithms&rdquo; book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

*UVa 340 Master-Mind Hints(例题3-4)

为什么要加星号?因为这道题的算法和书上大致上是差不多的,不一样的地方是我把输入数组换成读入的时候统计而已 但是在oj上一直通不过,这我就很懵逼了,也不想把书上的代码打一遍,反正基本上差不多,自己对照一下书上吧 至于为什么是取最小的一个?应该是“同时出现”,所以取比较小的数据相加得到数字的贡献 PS:如果发现代码有哪里出现问题请在评论里联系我,谢谢,因为我自己也不知道哪里的问题,oj上的测试数

Dream of Decision 325

紫书第三章训练2 暴力集

A - Master-Mind Hints MasterMind is a game for two players. One of them,Designer, selects a secret code. The other,Breaker, tries to break it. A code is no more than a row of colored dots. At the b...

weixin_34364135的博客 680

UVA340 UVALive5448 Master-Mind Hints【水题】

  MasterMind is a game for two players. One of them, Designer, selects a secret code. The other, Breaker, tries to break it. A code is no more than a row of colored dots. At the beginning of a game, t...

海岛Blog 1430

刷紫书第三章例题(例题3-4,3-5,3-6)

例题3-4 Master-Mind Hints UVA - 340发顺丰

ccnuacmhdu的博客 864

UVA 340 Master-Mind Hints 猜数字提示的游戏 简单模拟

题意:以前曾经玩过的一个游戏,就是猜数字,原来的游戏规则是这样的: 請寫一個猜數字遊戲,電腦會產生一個四位數且每個數字都不重複的數字讓使用 者去猜,使用者猜測一組數字後,電腦會顯示 ?A?B 的訊息,A 代表正確的數字 數目,B 代表數字正確但位置不對的數字數目。 这边有个在线玩的网站:请戳这里,可能玩过了就会有点感觉了。 题目有点不同,它给出的密码是多位的,然后给出一系列的猜测,让

水果君の日常 3165

340 - Master-Mind Hints

Master-Mind Hints  MasterMind is a game for two players. One of them, Designer, selects asecret code. The other, Breaker, tries to break it. A code is no morethan a row of colored dots. At

SD_Sunny_hui的专栏 1056

猜数字游戏的提示(Master-Mind Hints, UVa 340)

Written by Robert_Wang in Southwest University of Science And Technology.实现一个经典&quot;猜数字&quot;游戏。给定答案序列和用户猜的序列,统计有多少数字位置正确(A),有多少数字在两个序列都出现过但位置不对(B)。输入包含多组数据。每组输入第一行为序列长度n,第二行是答案序列,接下来是若干猜测序列。猜测序列全0时该组数据结束。n=0时...

乌云踏雪 736

Master-Mind Hints(猜数字游戏的提示)

MasterMind is a game for two players. One of them, Designer, selects a secret code. The other, Breaker, tries to break it. A code is no more than a row of colored dots. At the beginning of a game, the...

weixin_33716941的博客 297
上一篇: 程序员眼中的酒席
下一篇: while(cin>>val)的调用机理
synapse7
博客等级 码龄14年 432粉丝 701原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值