What's in a name?

The FBI is conducting surveillance on a known criminal hideout which serves as a communication centre for a number of men and women of nefarious intent. Using sophisticated decryption software and good old fashioned wiretaps, they are able to decode any email messages leaving the site. However, before any arrest warrants can be served, they must match actual names with the user IDs on the messages.

Problem “What’s in a name” located at Peking University.

Solution

""" solution.py """

# usage: python3 solution.py < input

import sys


with sys.stdin as infile:
    infile.readline()
    ids = {i: set() for i in infile.readline().split()}
    hideout = {}
    while (obs := infile.readline().strip()) != 'Q':
        action, who = obs.split()
        match action:
            case 'E':
                hideout[who] = True
            case 'L':
                hideout[who] = False
            case 'M' if ids[who]:
                absent = {i for i, j in hideout.items() if not j}
                ids[who] -= absent
            case 'M':
                present = {i for i, j in hideout.items() if j}
                ids[who] = present

hideout = {i: '???' for i in sorted(hideout)}

while True:
    for ident, crims in ids.items():
        if len(crims) == 1:
            (crim, ) = crims
            hideout[crim] = ident
            break
    else:
        break

    ids = {key: value - {crim} for key, value in ids.items()
           if key != ident}

for crim, ident in hideout.items():
    print(f'{crim}:{ident}')

Output

bonnie:fatman
booth:???
clyde:frenchie
knuckles:bigman
moriarty:???
mugsy:mangler
ugati:sinbad