Maintain a growing string under appends, insertions of the whole current string into a set, and queries counting how many stored strings are suffixes of the current string.
Medium7StringTrieNo attempts yetTime limit0.5sMemory limit256 MBK found a strange game on his computer. The game starts with a string S of length N (1≤N≤1000) and an empty set T. Three kinds of events happen while the game runs.
K wants to visit a famous castle near his hometown, so help him finish the game as fast as possible.
The first line contains two integers N and E. N is the length of the initial string S, and E is the number of events (E≤1200000).
The second line contains the string S. It consists of lowercase letters a-z only.
Each of the next E lines describes one event. Every such line starts with an integer p giving the event type.
For every event of type 3 in the input, print the answer as an integer on its own line.
T is a set, so adding the same string several times still leaves one copy in it.
The input is large, so fast input and output are required. In C use scanf and printf, in C++ put cin.tie(NULL); ios::sync_with_stdio(false); before reading, and in Java use BufferedReader and BufferedWriter.