Task
The Powerpuff girls have encountered a computer science problem at the ONI. They are not familiar with struct arbore and that's why they ask for your help.
You are given nodes, each node having a specific lowercase letter.
For queries, you have two types of queries you can receive:
- . Find the longest suffix of the nodes such that the words formed are identical. A word is defined as a chain of consecutive nodes. If we want to form a word from node to node , must be the child of node .
- . Node will change its character value to .
The Powerpuff girls ask you to give them a code with the best possible complexity so that they get the gold medal.
Input data
On the first line is an integer, , which is the number of nodes in the tree structure.
On the second line are letters, representing the letter for each node.
On the third line is the string , (, ), representing the father of node in the struct arbore.
On the fourth line is , representing the number of questions.
On the next lines are information about the queries to perform.
Output data
The program will display the query results one line at a time, in the order they appear in the input.
Constraints and clarifications
- ;
- ;
- The string will be indexed from .
# | Points | Constraints |
---|---|---|
0 | 0 | Exemple |
1 | 10 | |
2 | 40 | There is only type of queries |
3 | 50 | No additional constraints |
Comment
Due to the very large input data, we recommend using the following lines at the beginning of the main()
function in the C++ program:
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
Exemple 1
stdin
11
bfpfpprpbff
1 2 2 2 3 4 5 7 9 10
5
1 6 8
1 11 4
2 7 f
2 2 b
1 11 7
stdout
4
3
3
Explanation
The next picture shows our tree.
For the first question we observe that the longest suffix of the and nodes is formed by the sequence of nodes: , respectively: . We note that the node sequences and are also valid, but they do not have maximal length.
For the second question we observe that the longest suffix of nodes and is formed by the sequence of nodes: , respectively: . It can't be bigger, since the chain formed from node has reached the root.
Next we modify two nodes, so node goes from to , and node from to .
For the last question, the longest suffix will be , consisting of nodes , respectively: .
Exemple 2
stdin
21
lncnfshipesgoragaorjj
1 2 2 1 5 5 7 6 4 4 3 12 13 9 15 16 16 18 19 14
2
1 21 20
1 11 6
stdout
4
1