Extra Character
면접 대비시간 제한3초메모리 제한2048 MB
문자열의 Z-함수가 주어졌을 때, 첫 글자를 제거한 문자열의 Z-함수를 구하고 유일하게 정해지지 않는 값은 -1로 출력한다.
문제
Given a string of length , the Z-function of is a sequence of integers such that the -th element is equal to the largest nonnegative integer no greater than such that for all . For example, the -th element is if , and the first element is always .
Your friend recently sent you a string of length . 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 . For each position, output instead if the value on the specific position cannot be uniquely determined.
입력
The first line contains a single integer , the length of the original string. ()
The second line contains integers , the Z-function of the original string. ()
It is guaranteed that a string corresponding to the given Z-function exists.
출력
Output integers , the Z-function of the intended string .
If the value cannot be uniquely determined for some position, output for the position.