UVa 340 Master-Mind Hints (优化查找&复制数组)
340 - Master-Mind Hints
Time limit: 3.000 seconds
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
and a guess
, and are to determine the hint. A hint consists of a pair of numbers determined as follows.
A match is a pair (i,j),
and
, such that
. 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;
}
UVa 340 Master-Mind Hints (优化查找&复制数组)的更多相关文章
- 【例题3-4 UVA - 340】Master-Mind Hints
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 这里出现了没有在相同位置的只能唯一配对. 就是说 3322 2234 这种情况. 只有3个weak pair. 即key[1]=a[ ...
- UVa 340 Master-Mind Hints
蛋疼的题目描述,看了好长好长时间才看懂,题目本身是很简单的. Designer给出一串长度为N的Code,Breaker用Guess来破译. 对于两串数字,如果有同一列相等的数字,那么叫做strong ...
- UVa 340 Master-Mind Hints(猜数字游戏的提示)
题意 猜数字游戏 统计猜的数字有多少个数字位置正确 有多少个数字在答案中出现可是位置不对 每一个字符仅仅能匹配一次 直接匹配每位数 #include<cstdio> #includ ...
- UVa 340 - Master-Mind Hints 解题报告 - C语言
1.题目大意 比较给定序列和用户猜想的序列,统计有多少数字位置正确(x),有多少数字在两个序列中都出现过(y)但位置不对. 2.思路 这题自己思考的思路跟书上给的思路差不多.第一个小问题——位置正确的 ...
- UVA 340 Master-Mind Hints 猜密码游戏(水)
题意: 给一串密码(第一行),接着再给你很多行猜测,针对每行猜测,输出两个数字,分表代表:同一列上匹配的个数,不同列上匹配的个数.注:匹配指的是一次,一旦配对,不能再与其他配对. 思路: 每接受一行猜 ...
- 猜数字游戏的提示(Master-Mind Hints, UVa 340)
实现一个经典"猜数字"游戏. 给定答案序列和用户猜的序列,统计有多少数字位置正确 (A),有多少数字在两个序列都出现过但位置不对(B). 输入包含多组数据.每组输入第一行为序列长度 ...
- uva340 Master-Mind Hints (UVA - 340)
题目简要 题目意思很简单每个测试都由原题目在第一行,然后后面的都是去猜的答案,如果猜测的位置正确那么输出的结果的数对里面的第一个数就加一,如果仅答案正确(原题目里有这个数,但是位置不一样)那么就在输出 ...
- C - 啥~ 渣渣也想找玩数字 HDU - 2141(有序序列枚举 + 二分优化查找)
题目描述 可爱的演演又来了,这次他想问渣渣一题... 如果给你三个数列 A[],B[],C[],请问对于给定的数字 X,能否从这三个数列中各选一个,使得A[i]+B[j]+C[k]=X? 输入 多组数 ...
- UVa 10003 (可用四边形不等式优化) Cutting Sticks
题意: 有一个长为L的木棍,木棍中间有n个切点.每次切割的费用为当前木棍的长度.求切割木棍的最小费用. 分析: d(i, j)表示切割第i个切点到第j个切点这段所需的最小费用.则有d(i, j) = ...
随机推荐
- gis-矢量与栅格数据结构的比较
2.5矢量与栅格数据结构的比较 在计算机辅助制图和地理信息系统发展早期,最初引用的是矢量处理技术,栅格数据处理始于70年 代中期.几年以前,这两种数据结构势不两立,很难兼容,因此给数据利用带来许多不便 ...
- jquery动画总结
基本动画 show() //直接显示元素,没有动画 show(speed, [callback]) //有动画,有回调函数 hide() //直接隐藏元素,没有动画 hide(speed, [call ...
- 【USACO 3.2.6】香甜的黄油
[描述] 农夫John发现做出全威斯康辛州最甜的黄油的方法:糖.把糖放在一片牧场上,他知道N(1<=N<=500)只奶牛会过来舔它,这样就能做出能卖好价钱的超甜黄油.当然,他将付出额外的费 ...
- Spring依赖注入的三种方式
看过几篇关于Spring依赖注入的文章,自己简单总结了一下,大概有三种方式: 1.自动装配 通过配置applicationContext.xml中的标签的default-autowire属性,或者标签 ...
- id class
id 选择器可以为标有特定 id 的 HTML 元素指定特定的样式. HTML元素以id属性来设置id选择器,CSS 中 id 选择器以 "#" 来定义. 以下的样式规则应用于元素 ...
- WEB兼容性之JS
1. 获取iframe的window对象 //三种方法 window.iframeName window.frames[iframeName] document.getElementById(ifra ...
- TCP客户/服务器程序概述
一个回射服务器: 1)客户从标准输入读入一行文本,并写给服务器 2)服务器从网络输入读入这行文本,并回射给客户 3)客户从网络输入读入这行回射文本,并显示在标准输出上 回射输入行这样一个客户/服务器程 ...
- C# 基于委托的事件
事件基于多播委托的特性. 多播委托事实上就是一组类型安全的函数指针管理器,调用则执行顺序跳转到数组里所有的函数指针里执行. class Program { public class CarInfoEv ...
- IOS UITableView NSIndexPath属性讲解
IOS UITableView NSIndexPath属性讲解 查看UITableView的帮助文档我们会注意到UITableView有两个Delegate分别为:dataSource和deleg ...
- 监测div 元素 变动
$(div_fc_even).bind('DOMCharacterDataModified ', function(event) { } 试了很多..只有这个有效..下面这些测试了下 ,无反应 //D ...