# Moderate 猜测序列hit或是pseudo-hit @CareerCup

package Moderate;

import java.util.Random;

/**
* The Game of Master Mind is played as follows:

The computer has four slots containing balls that are red (R ), yellow (Y), green (G) or blue (B). For example, the computer might have RGGB (e.g., Slot #1 is red, Slots #2 and #3 are green, Slot #4 is blue).

You, the user, are trying to guess the solution. You might, for example, guess YRGB.When you guess the correct color for the correct slot, you get a “hit”. If you guess a color that exists but is in the wrong slot, you get a “pseudo-hit”. For example, the guess YRGB has 2 hits and one pseudo hit.

For each guess, you are told the number of hits and pseudo-hits. Write a method that, given a guess and a solution, returns the number of hits and pseudo hits.

Master Mind游戏规则如下：

4个槽，里面放4个球，球的颜色有4种，红(R )，黄(Y)，绿(G)，蓝(B)。比如， 给出一个排列RGGB，表示第一个槽放红色球，第二和第三个槽放绿色球，第四个槽放蓝色球。

*
*/
public class S17_5 {

public static class Result {
public int hits;
public int pseudoHits;

public Result(int h, int p) {
hits = h;
pseudoHits = p;
}

public Result() {
}

public String toString() {
return "(" + hits + ", " + pseudoHits + ")";
}
};

public static int code(char c) {
switch (c) {
case 'B':
return 0;
case 'G':
return 1;
case 'R':
return 2;
case 'Y':
return 3;
default:
return -1;
}
}

public static int MAX_COLORS = 4;

public static Result estimate(String guess, String solution) {
if (guess.length() != solution.length()) return null;
Result res = new Result();
int[] frequencies = new int[MAX_COLORS];

/* Compute hits and built frequency table */
for (int i = 0; i < guess.length(); i++) {
if (guess.charAt(i) == solution.charAt(i)) {
res.hits++;
} else {
/* Only increment the frequency table (which will be used for pseudo-hits) if
* it's not a hit. If it's a hit, the slot has already been "used." */
int code = code(solution.charAt(i));
if (code >= 0) {
frequencies[code]++;		// 把答案的分布存在frequencies数组中
}
}
}

/* Compute pseudo-hits */
for (int i = 0; i < guess.length(); i++) {
int code = code(guess.charAt(i));
if (code >= 0 && frequencies[code] > 0 && guess.charAt(i) != solution.charAt(i)) {
res.pseudoHits++;
frequencies[code]--;
}
}
return res;
}

/************************** TEST CODE **********************************/

public static char letterFromCode(int k) {
switch (k) {
case 0:
return 'B';
case 1:
return 'G';
case 2:
return 'R';
case 3:
return 'Y';
default:
return '0';
}
}

public static Result estimateBad(String g, String s) {
char[] guess = g.toCharArray();
char[] solution = s.toCharArray();
int hits = 0;
for (int i = 0; i < guess.length; i++) {
if (guess[i] == solution[i]) {
hits++;
solution[i] = '0';
guess[i] = '0';
}
}

int pseudohits = 0;

for (int i = 0; i < guess.length; i++) {
if (guess[i] != '0') {
for (int j = 0; j < solution.length; j++) {
if (solution[j] != '0') {
if (solution[j] == guess[i]) {
pseudohits++;
solution[j] = '0';
break;
}
}
}
}
}

return new Result(hits, pseudohits);
}

public static String randomString() {
int length = 4;
char[] str = new char[length];
Random generator = new Random();

for (int i = 0; i < length; i++) {
int v = generator.nextInt(4);
char c = letterFromCode(v);
str[i] = c;
}

return String.valueOf(str);
}

public static boolean test(String guess, String solution) {
Result res1 = estimate(guess, solution);
if (res1.hits == res2.hits && res1.pseudoHits == res2.pseudoHits) {
return true;
} else {
System.out.println("FAIL: (" + guess + ", " + solution + "): " + res1.toString() + " | " + res2.toString());
return false;
}
}

public static boolean testRandom() {
String guess = randomString();
String solution = randomString();
return test(guess, solution);
}

public static boolean test(int count) {
for (int i = 0; i < count; i++) {
if (!testRandom()) {
return true;
}
}
return false;
}

/********************** END TEST CODE ************************/

public static void main(String[] args) {
test(1000);
}
}


## HIT密码学实验1-4

• 2013年10月24日 18:06
• 1.21MB
• 下载

## EqualLogic配置初始化和加入Group HIT pdf

• 2014年01月29日 20:30
• 2.26MB
• 下载

## POJ 2296 Map Labeler / ZOJ 2493 Map Labeler / HIT 2369 Map Labeler / UVAlive 2973 Map Labeler

POJ 2296 Map Labeler / ZOJ 2493 Map Labeler / HIT 2369 Map Labeler / UVAlive 2973 Map Labeler（2-sat ...

## hit软件学院java实验FileCutter

• 2013年11月21日 19:01
• 15KB
• 下载

## Hit Refresh

• 2017年10月30日 12:09
• 551KB
• 下载

## Hit Fm联播网 1.4

• 2015年01月07日 16:36
• 11.61MB
• 下载

## HIT软件设计一代码

• 2013年10月24日 15:51
• 25KB
• 下载

## ACM计算几何模板__HIT_jerrybond

• 2014年03月21日 22:53
• 409KB
• 下载

举报原因： 您举报文章：Moderate 猜测序列hit或是pseudo-hit @CareerCup 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)