Castle

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 MB

Problem

K found a strange game on his computer. The game starts with a string SS of length NN (1N10001 \le N \le 1000) and an empty set TT. Three kinds of events happen while the game runs.

  • One character is appended to the end of SS, so the length of SS grows by 1.
  • The current SS is added to the set TT.
  • The game master asks: "how many strings in TT are suffixes of SS?" A suffix of SS is a substring that may start at any position of SS but must end at the last position of SS.

K wants to visit a famous castle near his hometown, so help him finish the game as fast as possible.

Input

The first line contains two integers NN and EE. NN is the length of the initial string SS, and EE is the number of events (E1200000E \le 1\,200\,000).

The second line contains the string SS. It consists of lowercase letters a-z only.

Each of the next EE lines describes one event. Every such line starts with an integer pp giving the event type.

  • If pp is 1, one character (a-z) follows on the same line. Append that character to the end of SS.
  • If pp is 2, add the current SS to TT.
  • If pp is 3, answer the question "how many strings in TT are suffixes of the current SS?".

Output

For every event of type 3 in the input, print the answer as an integer on its own line.

TT 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.