CF1312G.Autocompletion
省选/NOI-
通过率:0%
时间限制:7.00s
内存限制:512MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are given a set of strings S. Each string consists of lowercase Latin letters.
For each string in this set, you want to calculate the minimum number of seconds required to type this string. To type a string, you have to start with an empty string and transform it into the string you want to type using the following actions:
- if the current string is t, choose some lowercase Latin letter c and append it to the back of t, so the current string becomes t+c. This action takes 1 second;
- use autocompletion. When you try to autocomplete the current string t, a list of all strings s∈S such that t is a prefix of s is shown to you. This list includes t itself, if t is a string from S, and the strings are ordered lexicographically. You can transform t into the i-th string from this list in i seconds. Note that you may choose any string from this list you want, it is not necessarily the string you are trying to type.
What is the minimum number of seconds that you have to spend to type each string from S?
Note that the strings from S are given in an unusual way.
给你一个字符串集合 S,其中每个字符串均由小写拉丁字母组成。
对于该集合中的每个字符串,你需要计算出输入该字符串所需的最少秒数。输入一个字符串时,你必须从一个空字符串开始,并通过以下操作将其变换为你想要输入的字符串:
- 若当前字符串为 t,则可以选择某个小写拉丁字母 c,并将 c 追加到 t 的末尾,使当前字符串变为 t+c。此操作耗时 1 秒;
- 使用自动补全功能。当你尝试对当前字符串 t 进行自动补全时,系统会向你展示所有满足“t 是 s 的前缀”的字符串 s∈S 构成的列表。该列表包含 t 本身(若 t∈S),且列表中的字符串按字典序排列。你可以用 i 秒将 t 变换为该列表中第 i 个字符串(i 从 1 开始计数)。注意:你可以选择列表中任意一个字符串,不一定是你最终想要输入的目标字符串。
请问:输入集合 S 中每个字符串所需的最少秒数分别是多少?
注意:集合 S 中的字符串是以一种特殊方式给出的。
输入格式
The first line contains one integer n (1≤n≤106).
Then n lines follow, the i-th line contains one integer pi (0≤pi<i) and one lowercase Latin character ci. These lines form some set of strings such that S is its subset as follows: there are n+1 strings, numbered from 0 to n; the 0-th string is an empty string, and the i-th string (i≥1) is the result of appending the character ci to the string pi. It is guaranteed that all these strings are distinct.
The next line contains one integer k (1≤k≤n) — the number of strings in S.
The last line contains k integers a1, a2, ..., ak (1≤ai≤n, all ai are pairwise distinct) denoting the indices of the strings generated by above-mentioned process that form the set S — formally, if we denote the i-th generated string as si, then S=sa1,sa2,…,sak.
第一行包含一个整数 n(1≤n≤106)。
接下来有 n 行,第 i 行包含一个整数 pi(0≤pi<i)和一个小写拉丁字母 ci。这些行定义了某个字符串集合,其中 S 是该集合的一个子集,具体定义如下:共有 n+1 个字符串,编号从 0 到 n;第 0 个字符串为空字符串;对于 i≥1,第 i 个字符串由将字符 ci 追加到字符串 pi 后得到。保证所有这些字符串互不相同。
下一行包含一个整数 k(1≤k≤n)——即集合 S 中字符串的个数。
最后一行包含 k 个整数 a1, a2, ..., ak(1≤ai≤n,且所有 ai 两两不同),表示由上述过程生成的字符串中属于集合 S 的那些字符串的下标。形式化地说,若记第 i 个生成的字符串为 si,则 S={sa1,sa2,…,sak}。
输出格式
Print k integers, the i-th of them should be equal to the minimum number of seconds required to type the string sai.
输出 k 个整数,其中第 i 个整数应等于输入字符串 sai 所需的最少秒数。
输入输出样例
输入#1
10 0 i 1 q 2 g 0 k 1 e 5 r 4 m 5 h 3 p 3 e 5 8 9 1 10 6
输出#1
2 4 1 3 3
输入#2
8 0 a 1 b 2 a 2 b 4 a 4 b 5 c 6 d 5 2 3 4 7 8
输出#2
1 2 2 4 4
说明/提示
In the first example, S consists of the following strings: ieh, iqgp, i, iqge, ier.
在第一个例子中,S 包含以下字符串:ieh、iqgp、i、iqge、ier。
输入解题思路,AI测评打分。不知道怎么写?