Under construction

Still being built. Things may change

Adjacent Mastermind

Time limit1sMemory limit128 MB

Summary
Given pairs of target and guess letter strings, compute black, grey, and white Mastermind scores by matching exact, then adjacent, then distant letters in priority order.
Level

Medium6 of 10

Topics
String, Greedy, Simulation, Array
Solved
No attempts yet

Problem

Mastermind is a game about guessing a hidden ordered arrangement of colored pegs. Instead of physical pegs, uppercase letters from A to Z may be used as colors. One player secretly chooses the target arrangement, and another player submits a guess. The player who knows the target must compute the score for the guess.

In this variant, the score is computed in the following order.

  • The black score is the number of positions where the target and guess contain the same letter.
  • The grey score is the number of guess letters not counted as black that can be matched with an unused target letter of the same kind immediately to the left or right.
  • The white score is the number of guess letters not counted as black or grey that can be matched with an unused target letter of the same kind at least two positions away.

Each target letter can be matched at most once, and each guess letter can contribute to at most one score type. First fix the black score, then use the remaining letters to make as many grey matches as possible, and finally use the remaining letters to make as many white matches as possible. For each input line, output the score of the guess.

Input

The input contains several lines. Each line contains a target arrangement and a guess arrangement separated by one space. Each arrangement is a string of length between 2 and 50, inclusive, using uppercase letters from A to Z. On the same line, the target and guess always have the same length.

A line containing only # marks the end of input. Do not process that line.

Output

For each input line containing a target and a guess, output one line. The format is guess: b black, g grey, w white. Here guess is the guess arrangement from the input, and b, g, and w are the black, grey, and white scores.

Examples1

  1. Example 1

    Input
    ABCD AAAA
    ABCD ACEB
    ABCD EFBB
    ABCD ACBC
    ABCD BEAA
    ABCD ABCD
    ABABAA BCBCAA
    #
    
    Expected output
    AAAA: 1 black, 0 grey, 0 white
    ACEB: 1 black, 1 grey, 1 white
    EFBB: 0 black, 1 grey, 0 white
    ACBC: 1 black, 2 grey, 0 white
    BEAA: 0 black, 1 grey, 1 white
    ABCD: 4 black, 0 grey, 0 white
    BCBCAA: 2 black, 2 grey, 0 white