Police Hypothesis
Time limit2sMemory limit512 MB
Given a tree with one letter per vertex, count how often pattern P appears on the path between two vertices, and handle letter updates.
- Level
Hard8 of 10
- Topics
- Tree, String matching, Segment tree, DFS
- Solved
- No attempts yet
Problem
The public transport system of Nlogônia has an express network that connects the country's main tourist sites. bullet trains connect attractions so that starting from any one tourist site, you can reach every other site using only this express network.
Graffiti is common at train stations, as it is anywhere in the world. The police noticed that every station has exactly one letter painted in a specific style. They suspect criminals are changing the graffiti to communicate, so they decided to build a system that monitors the graffiti and its changes.
Given a pattern , the connections between stations, and the suspicious letter at each station, write a program that handles the following operations.
- 1 u v: Print how many times the pattern occurs on the path from to , reading the letters of consecutive vertices along the path.
- 2 u x: Change the suspicious letter at station to .
Input
The first line contains two integers and (), the number of stations and the number of operations. The second line contains the monitored pattern (). The third line contains a string of characters, the letters initially associated with each station. Each of the next lines contains two integers and , meaning there is a bullet train between stations and . The next lines describe the operations.
Output
For each operation of type 1, print one line containing the number of occurrences of the pattern on the analyzed path.