#718
Unrated
Vocabulary Quiz
시간 제한
2s
메모리 제한
1024MB
제출
0
정답
0
맞힌 사람
0
정답 비율
0.0%

문제

Bessie is helping Elsie with her upcoming vocabulary quiz. The words to be tested will be drawn from a bank of MM distinct words, where no word in the bank is a prefix of another word in the bank.

While the bank is nonempty, Bessie will select a word from the bank, remove it from the bank, and read it to Elsie one character at a time from left to right. Elsie's task is to tell Bessie once she can uniquely identify it, after which Bessie will stop reading.

Bessie has already decided to read the words from the word bank in the order w1,w2,,wMw_1,w_2,\dots,w_M. If Elsie answers as quickly as possible, how many characters of each word will Bessie read?

The words are given in a compressed format. We will first define N+1N+1 (1N1061\le N\le 10^6) distinct words, and then the word bank will consist of all those words that are not a prefix of another word. The words are defined as follows:

Initially, the 00th word will be the empty string. Then for each 1iN1\le i\le N, the iith word will be equal to the pip_ith word plus an additional character at the end (0pi<i0\le p_i<i). The characters are chosen such that all N+1N+1 words are distinct.

입력

The first line contains NN, where N+1N+1 is the number of words given in the compressed format.

The next line contains p1,p2,,pNp_1,p_2,\dots,p_N where pip_i represents that the ii-th word is formed by taking the pip_i-th word and adding a single character to the end.

Let MM be the number of words that are not a prefix of some other word. The next MM lines will contain w1,w2,,wMw_1,w_2,\dots,w_M, meaning that the wiw_ith word will be the iith to be read. It is guaranteed that the words to be read form a permutation of the words in the word bank.

출력

Output MM lines, where the iith line contains the number of characters of the iith word that Bessie reads.

예제 입력 1

5
0 1 2 3 4
5

예제 출력 1

0

예제 입력 2

4
0 0 1 1
4
3
2

예제 출력 2

2
1
0

예제 입력 3

4
0 0 1 1
2
3
4

예제 출력 3

1
2
0

점수

Inputs 4-5: No word has length greater than 2020.Inputs 6-10: The sum of the lengths of all words in the word bank does not exceed 10710^7.Inputs 11-18: No additional constraints.

코드 제출

코드를 제출하려면 로그인이 필요합니다.

로그인
내 제출
제출 내역이 없습니다.
맞은 사람
아직 맞은 사람이 없습니다.
난이도 투표
Unrated0명 투표
로그인 후 AC 받으면 투표할 수 있습니다.
전체 제출
제출 내역이 없습니다.