#P11087. [ROI 2019] 考古 (Day 2)

[ROI 2019] 考古 (Day 2)

Background

Translated from ROI 2019 D2T4

In the year 3019, while excavating the ancient city of Innopolis, archaeologists discovered an ancient artifact—a hard drive. It contained a file which is believed to include the texts of all ROI problems.

After studying the file, it was found that the information was encoded as a string tt consisting of English letters. The problem texts are very long and contain many repeated parts, so the file was stored on the hard drive in compressed form. The file is decompressed using the following algorithm:

During decompression, a string tt consisting of lowercase English letters will be produced. Initially, the string tt is empty. The compressed file contains nn blocks, and you must process each block in order. Each block is of one of the following two types.

  • Block 1 w, where ww is a string. When processing such a block, append the string ww to the end of tt.
  • Block 2 pos len, where pospos and lenlen are positive integers. Assume that the characters of tt are numbered starting from 11. When processing such a block, append to the end of tt the lenlen consecutive characters starting from position pospos in tt, in order. If lenlen is large enough, some of the characters that were just appended may be used again while processing the same block.

Problem Description

The archaeologists decided to count how many times an algorithm was tested in ROI. For this, they constructed a string pp consisting of lowercase English letters, and they want to find the number of occurrences of this string in the decompressed string tt.

A string pp of length mm occurs as a substring starting at position ii if the mm consecutive characters starting from the ii-th character equal pp. For example, the string aba occurs three times as a substring in the string ababaaba, starting at characters 1,3,61,3,6.

Write a program to determine the number of occurrences of the given string pp in the decompressed string tt.

Input Format

The first line contains two non-negative integers mm and nn, representing the length of the string pp and the number of blocks in the compressed text, respectively.

The second line contains a non-empty string pp, consisting of lowercase English letters.

The next nn lines contain the descriptions of the blocks in the format given in the Background section.

Output Format

Output one integer: the number of occurrences of the string pp in the text.

3 4
aba
1 ab
2 1 3
2 3 3
2 1 8
6

Hint

Sample Explanation:

The decompression process of tt is as follows:

aba occurs 66 times in ababaabaababaaba.

Constraints:

Let the length of the string after decompressing the first ii blocks be LiL_i, and let the type of the ii-th block be typeitype_i.

Subtask Points mm\le nn\le LnL_n\le Other Special Properties
11 66 20002000 11 10001000
22 1010 10510^5 20002000 10610^6
33 20002000 101010^{10} i>1,typei=2,posi=1,L1leni\forall i>1,type_i=2,pos_i=1,L_1\mid len_i
44 posi=Li1pos_i=L_{i-1}
55 2020 posi=1,leni107pos_i=1,len_i\le10^7
66 44 20002000
77 1010 2020 pp contains only the letter a, posi+leni1Li1pos_i+len_i-1\le L_{i-1}
88 66 posi+leni1Li1pos_i+len_i-1\le L_{i-1}
99 22 11 20002000 pp contains only the letter a
1010 44 2020
1111 55
1212 1414 10510^5
1313 99 2×1052\times10^5 1000010000 101510^{15}

For 100%100\% of the testdata, w2×105\sum w\leq 2\times 10^5

Translated by ChatGPT 5