Compression and Expansion (Codeforces 1523C)
Tags: Greedy, trees

  William is a huge fan of planning ahead. That is why he starts his morning routine by creating a nested list of upcoming errands.A valid nested list is any list which can be created from a list with one item "1" by applying some operations. Each operation inserts a new item into the list, on a new line, just after one of existing items $$$a_1 \,.\, a_2 \,.\, a_3 \,.\, \,\cdots\, \,.\,a_k$$$ and can be one of two types:   Add an item $$$a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, a_k \,.\, 1$$$ (starting a list of a deeper level), or  Add an item $$$a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, (a_k + 1)$$$ (continuing the current level).  Operation can only be applied if the list does not contain two identical items afterwards. And also, if we consider every item as a sequence of numbers, then the sequence of items should always remain increasing in lexicographical order. Examples of valid and invalid lists that are shown in the picture can found in the "Notes" section.When William decided to save a Word document with the list of his errands he accidentally hit a completely different keyboard shortcut from the "Ctrl-S" he wanted to hit. It's not known exactly what shortcut he pressed but after triggering it all items in the list were replaced by a single number: the last number originally written in the item number.William wants you to help him restore a fitting original nested list.

Input: Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10$$$). Description of the test cases follows.The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^3$$$), which is the number of lines in the list.Each of the next $$$n$$$ lines contains a single integer $$$a_i$$$ ($$$1 \le a_i \le n$$$), which is what remains of William's nested list.It is guaranteed that in each test case at least one fitting list exists.It is guaranteed that the sum of values $$$n$$$ across all test cases does not exceed $$$10^3$$$.

Output: For each test case output $$$n$$$ lines which represent a valid nested list, which could become the data provided to you by William.If there are multiple answers, print any.

Note: In the second example test case one example of a fitting list is:11.1 1.1.11.1.21.21.2.122.12.2This list can be produced by using the sequence of operations shown below:    Original list with a single item $$$1$$$.  Insert item $$$2$$$ by using the insertion operation of the second type after item $$$1$$$.  Insert item $$$1.1$$$ by using the insertion operation of the first type after item $$$1$$$.  Insert item $$$1.2$$$ by using the insertion operation of the second type after item $$$1.1$$$.  Insert item $$$1.1.1$$$ by using the insertion operation of the first type after item $$$1.1$$$.  Insert item $$$1.1.2$$$ by using the insertion operation of the second type after item $$$1.1.1$$$.  Insert item $$$1.2.1$$$ by using the insertion operation of the first type after item $$$1.2$$$.  Insert item $$$2.1$$$ by using the insertion operation of the first type after item $$$2$$$.  Insert item $$$2.2$$$ by using the insertion operation of the second type after item $$$2.1$$$. 

Sample Inputs:
2
4
1
1
2
3
9
1
1
1
2
2
1
2
1
2

Sample Outputs:
1
1.1
1.2
1.3
1
1.1
1.1.1
1.1.2
1.2
1.2.1
2
2.1
2.2

Solution (Python):
for t in range(int(input())):
    n = int(input())
    ans = ['']
    prev = 0
    d = {}
    stack = []
    for i in range(n):
        l = int(input())
        if l == 1:
            ans.append(ans[-1] + '.1')
            stack.append(ans[-1])
            d[ans[-1]] = 1
        else:
            p = stack[-1].rfind('.')
            while stack[-1][p + 1:] != str(l - 1) or d.get(stack[-1][:p + 1] + str(l)) != None:
                stack.pop()
                p = stack[-1].rfind('.')
            ans.append(ans[-1][:p + 1] + str(l))
            stack.append(ans[-1])
            d[ans[-1]] = 1
    for i in range(n + 1):
        print(ans[i][1:])

Solution (Python):
import sys
p2D = lambda x: print(*x, sep='\n')

def II():
    return int(sys.stdin.buffer.readline())

def MI():
    return map(int, sys.stdin.buffer.readline().split())

def LI():
    return list(map(int, sys.stdin.buffer.readline().split()))

def LLI(rows_number):
    return [LI() for _ in range(rows_number)]

def BI():
    return sys.stdin.buffer.readline().rstrip()

def SI():
    return sys.stdin.buffer.readline().rstrip().decode()

def li():
    return [int(i) for i in input().split()]

def lli(rows):
    return [li() for _ in range(rows)]

def si():
    return input()

def ii():
    return int(input())

def ins():
    return input().split()

def drank(d, processing, da, rank):
    tmp = 10 ** 9
    if len(d[da]) == 1:
        return 1
    for di in d[da]:
        if processing[di - 1] == 0:
            processing[di - 1] = 1
            tmp = min(tmp, drank(d, processing, di, rank))
            processing[di - 1] = 0
    rank[da - 1] = tmp + 1
    return tmp + 1

def binary_search(n, a):
    l = len(a)
    low = 0
    high = l - 1
    while high >= low:
        mid = (high + low) // 2
        if a[mid] == n:
            return mid
        elif a[mid] > n:
            high = mid - 1
        else:
            low = mid + 1
    return -1
mod = 10 ** 9 + 7

def solve():
    n = II()
    ans = ['']
    d = {}
    stack = []
    for i in range(n):
        l = II()
        if l == 1:
            ans.append(ans[-1] + '.1')
            stack.append(ans[-1])
            d[ans[-1]] = 1
        else:
            p = stack[-1].rfind('.')
            while stack[-1][p + 1:] != str(l - 1) or d.get(stack[-1][:p + 1] + str(l)) != None:
                stack.pop()
                p = stack[-1].rfind('.')
            ans.append(ans[-1][:p + 1] + str(l))
            stack.append(ans[-1])
            d[ans[-1]] = 1
    for i in range(1, n + 1):
        print(ans[i][1:])

def main():
    for _ in range(II()):
        solve()
if __name__ == '__main__':
    main()

Solution (Python):
def ss(lst, l):
    y = lst[-1]
    while y:
        w = ''
        while y and y[-1] != '.':
            w = y[-1] + w
            y = y[:-1]
        if int(w) == l - 1:
            return y + str(l)
        if y:
            y = y[:-1]
    return str(l)
for _ in range(int(input())):
    n = int(input())
    lst = []
    x = 0
    l = int(input())
    lst.append('1')
    for i in range(n - 1):
        l = int(input())
        if l == 1:
            lst.append(lst[-1] + '.1')
        else:
            s = ss(lst, l)
            lst.append(s)
        x = l
    print(*lst)