Decode String (Codeforces 1729B)
Tags: Greedy, strings

Polycarp has a string $$$s$$$ consisting of lowercase Latin letters.He encodes it using the following algorithm.He goes through the letters of the string $$$s$$$ from left to right and for each letter Polycarp considers its number in the alphabet:  if the letter number is single-digit number (less than $$$10$$$), then just writes it out;  if the letter number is a two-digit number (greater than or equal to $$$10$$$), then it writes it out and adds the number 0 after. For example, if the string $$$s$$$ is code, then Polycarp will encode this string as follows:  'c' — is the $$$3$$$-rd letter of the alphabet. Consequently, Polycarp adds 3 to the code (the code becomes equal to 3);  'o' — is the $$$15$$$-th letter of the alphabet. Consequently, Polycarp adds 15 to the code and also 0 (the code becomes 3150);  'd' — is the $$$4$$$-th letter of the alphabet. Consequently, Polycarp adds 4 to the code (the code becomes 31504);  'e' — is the $$$5$$$-th letter of the alphabet. Therefore, Polycarp adds 5 to the code (the code becomes 315045). Thus, code of string code is 315045.You are given a string $$$t$$$ resulting from encoding the string $$$s$$$. Your task is to decode it (get the original string $$$s$$$ by $$$t$$$).

Input: The first line of the input contains an integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of test cases in the input.The descriptions of the test cases follow.The first line of description of each test case contains one integer $$$n$$$ ($$$1 \le n \le 50$$$) — the length of the given code.The second line of the description of each test case contains a string $$$t$$$ of length $$$n$$$ — the given code. It is guaranteed that there exists such a string of lowercase Latin letters, as a result of encoding which the string $$$t$$$ is obtained.

Output: For each test case output the required string $$$s$$$ — the string that gives string $$$t$$$ as the result of encoding. It is guaranteed that such a string always exists. It can be shown that such a string is always unique.

Note: The first test case is explained above.In the second test case, the answer is aj. Indeed, the number of the letter a is equal to $$$1$$$, so 1 will be appended to the code. The number of the letter j is $$$10$$$, so 100 will be appended to the code. The resulting code is 1100.There are no zeros in the third test case, which means that the numbers of all letters are less than $$$10$$$ and are encoded as one digit. The original string is abacaba.In the fourth test case, the string $$$s$$$ is equal to ll. The letter l has the number $$$12$$$ and is encoded as 120. So ll is indeed 120120.

Sample Inputs:
9
6
315045
4
1100
7
1213121
6
120120
18
315045615018035190
7
1111110
7
1111100
5
11111
4
2606

Sample Outputs:
code
aj
abacaba
ll
codeforces
aaaak
aaaaj
aaaaa
zf

Solution (Python):
def BoogeyMan() -> None:
    """"""
    n2a = {v: chr(k) for (k, v) in zip(range(97, 97 + 27), range(1, 27))}
    for _ in range(ii()):
        length = ii()
        s = si()
        length -= 1
        sb = ''
        while length >= 0:
            if s[length] == '0':
                n = int(s[length - 2] + s[length - 1])
                if s[length - 1] != '0':
                    sb += n2a[n]
                elif s[length - 2] == '1':
                    sb += n2a[10][::-1]
                else:
                    sb += n2a[20][::-1]
                length -= 2
            else:
                sb += n2a[int(s[length])]
            length -= 1
        p(sb[::-1])
if __name__ == '__main__':
    import os, sys, math, itertools, bisect
    from collections import deque, defaultdict, OrderedDict, Counter
    (ii, si) = (lambda : int(input()), lambda : input())
    (mi, msi) = (lambda : map(int, input().strip().split(' ')), lambda : map(str, input().strip().split(' ')))
    (li, lsi) = (lambda : list(mi()), lambda : list(msi()))
    (out, export, p, p1) = ([], lambda : print('\n'.join(map(str, out))), lambda x: out.append(x), lambda array: p(' '.join(map(str, array))))
    try:
        from baba_yaga import L, cmdIO, _generator_

        class _BoogeyMan_:

            def __init__(self) -> None:
                self.launchers = [cmdIO(), BoogeyMan(), export(), _generator_()]

            def init(self) -> None:
                for _ in self.launchers:
                    yield _
                    assert not _, 'EOF'

            def container(fun):

                def wrapper():
                    print(f'Success', end='\n')
                    fun()
                return wrapper

        @_BoogeyMan_.container
        def assembler():
            _BoogeyMan_().init()
        assembler()
    except (FileNotFoundError, ModuleNotFoundError):
        input = sys.stdin.readline
        BoogeyMan()
        export()

Solution (Python):
t = int(input())
for i in range(t):
    n = int(input())
    s1 = input()
    s1 = s1.replace('100', 'j')
    s1 = s1.replace('200', 't')
    s1 = s1.replace('110', 'k')
    s1 = s1.replace('120', 'l')
    s1 = s1.replace('130', 'm')
    s1 = s1.replace('140', 'n')
    s1 = s1.replace('150', 'o')
    s1 = s1.replace('160', 'p')
    s1 = s1.replace('170', 'q')
    s1 = s1.replace('180', 'r')
    s1 = s1.replace('190', 's')
    s1 = s1.replace('210', 'u')
    s1 = s1.replace('220', 'v')
    s1 = s1.replace('230', 'w')
    s1 = s1.replace('240', 'x')
    s1 = s1.replace('250', 'y')
    s1 = s1.replace('260', 'z')
    s1 = s1.replace('1', 'a')
    s1 = s1.replace('2', 'b')
    s1 = s1.replace('3', 'c')
    s1 = s1.replace('4', 'd')
    s1 = s1.replace('5', 'e')
    s1 = s1.replace('6', 'f')
    s1 = s1.replace('7', 'g')
    s1 = s1.replace('8', 'h')
    s1 = s1.replace('9', 'i')
    print(s1)

Solution (Python):
for _ in range(int(input())):
    n = int(input())
    s = input()
    s = s.replace('100', 'j').replace('200', 't').replace('110', 'k').replace('120', 'l').replace('130', 'm').replace('140', 'n').replace('150', 'o').replace('160', 'p').replace('170', 'q').replace('180', 'r').replace('190', 's').replace('210', 'u').replace('220', 'v').replace('230', 'w').replace('240', 'x').replace('250', 'y').replace('260', 'z')
    s = s.replace('1', 'a').replace('2', 'b').replace('3', 'c').replace('4', 'd').replace('5', 'e').replace('6', 'f').replace('7', 'g').replace('8', 'h').replace('9', 'i')
    print(s)