Coding/KOI

백준 KOI 2596번 - 편지 letter

xeohyuni 2023. 10. 8. 21:26

The given text is in Korean and describes a communication system where letters A, B, C, D, E, F, G, H are represented by sequences of six 0s and 1s. Each letter has a specific code, and there's an agreement that if there's an error in communication where only one digit is incorrect in the received code, it can still be understood.

Here's the English translation of the provided text:

Byunhyun sends letters A, B, C, D, E, F, G, H to Ji-eun every day in the form of secret messages written with the characters A, B, C, D, E, F, G, H represented by sequences of six 0s and 1s. The agreement between them is as follows:

A 000000
B 001111
C 010011
D 011100
E 100110
F 101001
G 110101
H 111010

If Byunhyun sends “001111000000011100” one day, Ji-eun understands it as “BAD.” However, due to the well-made agreement between them, if there is a communication problem resulting in only one digit being incorrectly received in the sequence of six digits representing a character, Ji-eun can still figure out the original intended character.

For example, if Jieun receives “000100,” where only one digit differs from A and the rest differ by at least two digits, Ji- eun recognizes it as A.

However, in cases where all six digits are different, like “111111,” Ji-eun cannot determine the character. For instance, if Ji-eun receives “011111000000111111000000111111,” after BA, there's an indistinguishable character. In such cases, the program should output the position of the first occurrence of this situation, which is 3 in this example.

Write a program to analyze the received letters and output the recognized characters or, in the case of an unknown character, output the position of its first occurrence.

 Input

The first line contains the number of sent characters (less than 10). The next line provides a sequence of six times the number of characters as input.

Output

Outputs the characters understood by the author from the given input, or, in the case of unknown characters, outputs the first occurrence of such occurrences.

Input/Output

3
001111000000011100
BAD
5
011111000000111111000000111111
3

Code

len_word = int(input())
num_word = input()
word_list = []
vocab = {"000000": "A","001111":"B","010011":"C","011100":"D","100110":"E","101001":"F","110101":"G","111010":"H"}
DM = str()     

for i in range(0,len(num_word),6):
    word_list.append(num_word[i:i+6])
for word in word_list:
    if vocab.get(word):
        DM += vocab[word]
    else:
        temp = ""
        for letter in vocab.keys():
            comp = bin(int(letter,2)^int(word,2))
            if comp.count("1") == 1:
                temp = vocab[letter]
            
        if len(temp) != 0:
            DM += temp
        else:
            print(len(DM)+1)
            break
if len(DM) == len_word:
    print(DM)

The code is programmed into a corresponding series of letters using a predefined vocabulary. The program starts by reading the length of the original word (len_word) and the binary sequence (num_word). It initializes an empty list (word_list) to store the binary codes of individual letters and a dictionary (vocab) to map binary codes to letters. The variable DM is initialized as an empty string to store the decoded message. The code then splits the binary sequence into 6-character segments and appends them to word_list. For each binary code in word_list, the program checks if it exists in the vocabulary (vocab). If found, the corresponding letter is appended to DM. If not found, the program attempts error correction by XORing the received code with each vocabulary entry and identifying a single-bit difference. If a correction is found, the corrected letter is appended to DM. If no correction is found, the program prints the position of the first error and exits. Finally, if the length of DM matches the expected word length, it is printed as the final output.

'Coding > KOI' 카테고리의 다른 글

[Baekjoon, KOI] 20186 - Choosing Numbers  (0) 2023.04.16