Fixed Prefix Permutations (Codeforces 1792D)
Tags: Math, hashing

You are given $$$n$$$ permutations $$$a_1, a_2, \dots, a_n$$$, each of length $$$m$$$. Recall that a permutation of length $$$m$$$ is a sequence of $$$m$$$ distinct integers from $$$1$$$ to $$$m$$$.Let the beauty of a permutation $$$p_1, p_2, \dots, p_m$$$ be the largest $$$k$$$ such that $$$p_1 = 1, p_2 = 2, \dots, p_k = k$$$. If $$$p_1 \neq 1$$$, then the beauty is $$$0$$$.The product of two permutations $$$p \cdot q$$$ is a permutation $$$r$$$ such that $$$r_j = q_{p_j}$$$.For each $$$i$$$ from $$$1$$$ to $$$n$$$, print the largest beauty of a permutation $$$a_i \cdot a_j$$$ over all $$$j$$$ from $$$1$$$ to $$$n$$$ (possibly, $$$i = j$$$).

Input: The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of testcases.The first line of each testcase contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 5 \cdot 10^4$$$; $$$1 \le m \le 10$$$) — the number of permutations and the length of each permutation.The $$$i$$$-th of the next $$$n$$$ lines contains a permutation $$$a_i$$$ — $$$m$$$ distinct integers from $$$1$$$ to $$$m$$$.The sum of $$$n$$$ doesn't exceed $$$5 \cdot 10^4$$$ over all testcases.

Output: For each testcase, print $$$n$$$ integers. The $$$i$$$-th value should be equal to the largest beauty of a permutation $$$a_i \cdot a_j$$$ over all $$$j$$$ ($$$1 \le j \le n$$$).


Sample Inputs:
3
3 4
2 4 1 3
1 2 4 3
2 1 3 4
2 2
1 2
2 1
8 10
3 4 9 6 10 2 7 8 1 5
3 9 1 8 5 7 4 10 2 6
3 10 1 7 5 9 6 4 2 8
1 2 3 4 8 6 10 7 9 5
1 2 3 4 10 6 8 5 7 9
9 6 1 2 10 4 7 8 3 5
7 9 3 2 5 6 4 8 1 10
9 4 3 7 5 6 1 10 8 2

Sample Outputs:
1 4 4 
2 2 
10 8 1 6 8 10 1 7

Solution (Python):
import sys
input = sys.stdin.readline
for _ in range(int(input())):
    (n, m) = map(int, input().split())
    T = {}
    A = []
    for _ in range(n):
        I = list(map(int, input().split()))
        J = [0] * (m + 1)
        for i in range(m):
            J[I[i]] = i + 1
        D = T
        for num in J[1:]:
            if not num in D:
                D[num] = {}
            D = D[num]
        A.append(I)
    Ans2 = []
    for data in A:
        Ans = 0
        D = T
        for num in data:
            if num in D:
                Ans += 1
                D = D[num]
            else:
                break
        Ans2.append(Ans)
    print(*Ans2)

Solution (Python):
import sys
input = sys.stdin.readline
for _ in range(int(input())):
    (n1, m1) = map(int, input().split())
    T = {}
    A = []
    for _ in range(n1):
        I = list(map(int, input().split()))
        J = [0] * (m1 + 1)
        for i in range(m1):
            J[I[i]] = i + 1
        D = T
        for num in J[1:]:
            if not num in D:
                D[num] = {}
            D = D[num]
        A.append(I)
    Ans2 = []
    for data in A:
        Ans = 0
        D = T
        for num in data:
            if num in D:
                Ans += 1
                D = D[num]
            else:
                break
        Ans2.append(Ans)
    print(*Ans2)

Solution (Python):
import sys
input = sys.stdin.readline
for _ in range(int(input())):
    (n, m) = map(int, input().split())
    t = {}
    y = []
    for _ in range(n):
        z = list(map(int, input().split()))
        q = [0] * (m + 1)
        for i in range(m):
            q[z[i]] = i + 1
        x = t
        for num in q[1:]:
            if not num in x:
                x[num] = {}
            x = x[num]
        y.append(z)
    ans2 = []
    for data in y:
        ans = 0
        x = t
        for num in data:
            if num in x:
                ans += 1
                x = x[num]
            else:
                break
        ans2.append(ans)
    print(*ans2)