Extra Character

면접 대비

시간 제한3초메모리 제한2048 MB

요약
문자열의 Z-함수가 주어졌을 때, 첫 글자를 제거한 문자열의 Z-함수를 구하고 유일하게 정해지지 않는 값은 -1로 출력한다.
난이도

어려움10점 중 9점

유형
문자열 매칭, 문자열, 구현
정답자
아직 제출이 없습니다

문제

Given a string ss of length nn, the Z-function of ss is a sequence of nn integers such that the ii-th element is equal to the largest nonnegative integer xx no greater than nn such that s_j=s_i+j−1s\_j=s\_{i+j-1} for all 1≤j≤x1 \le j \le x. For example, the ii-th element is 00 if s_i≠s_1s\_i \neq s\_1, and the first element is always nn.

Your friend recently sent you a string ss of length nn. He needs to compute the Z-function of this string but does not have enough time to compute it himself. You quickly compute it for him and send it back to your friend.

However, it seems like your friend made a mistake; he made a typo, and it turned out that the string he sent you had an extra character in the front. Unfortunately, neither you nor your friend kept a copy of the original string. Still, you want to know the Z-function of the intended string.

Please find the Z-function of the intended string t=s_2…nt=s\_{2\ldots n}. For each position, output −1-1 instead if the value on the specific position cannot be uniquely determined.

입력

The first line contains a single integer nn, the length of the original string. (2≤n≤1062 \leq n \leq 10^6)

The second line contains nn integers p_1,p_2,⋯ ,p_np\_1, p\_2, \cdots, p\_n, the Z-function of the original string. (0≤p_i≤n0 \leq p\_i \leq n)

It is guaranteed that a string corresponding to the given Z-function exists.

출력

Output n−1n - 1 integers q_1,q_2,⋯ ,q_n−1q\_1, q\_2, \cdots, q\_{n-1}, the Z-function of the intended string s_2…ns\_{2\ldots n}.

If the value cannot be uniquely determined for some position, output −1-1 for the position.

예제3

  1. 예제 1

    입력
    5
    5 4 3 2 1
    
    예상 출력
    4 3 2 1
    
  2. 예제 2

    입력
    7
    7 2 1 0 2 1 0
    
    예상 출력
    6 1 0 -1 1 0
    
  3. 예제 3

    입력
    7
    7 0 1 0 3 0 1
    
    예상 출력
    6 0 0 0 2 0