Idea Search
Time limit5sMemory limit1024 MB
Given blocks that append literal strings or copy earlier substrings of the growing text, count how many times pattern p occurs in the final text.
- Level
Hard8 of 10
- Topics
- String matching, String, Implementation
- Solved
- No attempts yet
Problem
In the year 3019, archaeologists dug up an ancient city called Innopolis and found a hard disk. It holds a file that probably contains the texts of all problems from the All-Russian informatics olympiads.
After studying the file, they found that its contents encode a string of lowercase English letters. The problem texts are quite long and contain many repetitions, so the file was stored on disk in compressed form. The following algorithm unpacks it.
During unpacking, a string of lowercase English letters is built. It starts empty. The compressed file consists of blocks, which must be processed in order. Each block has one of two types.
- Block
1 $w$, where is a string. Processing this block appends to the end of . - Block
2 $pos$ $len$, where and are positive integers. Number the characters of starting from 1. Processing this block appends, one by one, consecutive characters of starting at position to the end of . If is large enough, some of the characters just appended may be used again while processing the same block.
The researchers want to know how many times an idea appeared in the olympiads. They form a string of lowercase English letters and count how many times occurs as a substring of the unpacked string .
A string of length occurs in starting at position if the consecutive characters of starting at the -th character form . For example, aba occurs in ababaaba three times, at positions 1, 3, and 6.
Write a program that counts the occurrences of in the unpacked string .
Input
The first line contains natural numbers and , the length of string and the number of blocks in the compressed text (, ).
The second line contains the nonempty string , which consists of lowercase English letters.
Each of the next lines describes one block in the format above. For blocks of the first type, the appended string is nonempty, and the total length of all strings in blocks of the first type does not exceed . For blocks of the second type, when the block is processed, already has at least characters. The length of the unpacked text does not exceed characters.
Output
Print one integer, the number of occurrences of the string in the text.
Hint
Unpacking the example file produces these strings in turn: empty string ab ababa ababaaba ababaabaababaaba.
The string aba occurs in ababaabaababaaba 6 times.