#P5287. [HNOI2019] JOJO

[HNOI2019] JOJO

Description

To prevent too many words from blocking the manga content, the new manga plans to use xx Ora or xx Muda to represent that there are xx occurrences of Ora or Muda.

To simplify the content, we now use letters to represent the shouted words.

We use numbers and letters to represent a string. For example, 2 a 3 b represents the string aabbbaabbb.

At the beginning, there is no text in the manga. Next, you need to perform nn operations in order. There are only 22 types of operations in total:

  • The first type: 1 x c adds xx copies of cc to the current manga, meaning appending xx characters cc to the end of the current string. It is guaranteed that the current string is empty, or the last character of the string is not cc.
  • The second type: 2 x thinks the manga was not drawn well and restores it to the state after the xx-th operation, meaning restoring the string to the state after the xx-th operation. If x=0x=0, the string becomes the empty string.

If the current string is bbaabbbbbaabbb, and after the 44-th operation the string is bbbb, then 2 4 will change bbaabbbbbaabbb into bbbb. It is guaranteed that xx is less than the current operation index.

As everyone knows, Jotaro Kujo is very smart. Now that Dio has been defeated, he starts thinking about some problems in his manga:

For each prefix AA of a string, there is a longest prefix BB that is shorter than it and matches a suffix of prefix AA. Let the length of this longest prefix BB be LL. When LL is 00, it means BB is an empty string.

After each operation, you need to compute the sum of LL over all prefixes of the current string, take it modulo 998244353998244353, and output it to tell Jotaro Kujo, so that he can compare it with the answer computed by his Star Platinum.

For example, for bbaaabbabbaaabba, the values of LL are 0,1,0,0,0,1,2,30, 1, 0, 0, 0, 1, 2, 3, so the answer for this string is 77.

Input Format

The first line contains a positive integer nn, representing the number of operations.

The next nn lines each contain one operation. The operation formats are as described in the statement, for example:

  • 1 x c
  • 2 x

It is guaranteed that the input is valid.

Output Format

The output contains exactly nn lines. The ii-th line contains one integer, representing the answer after the first ii operations.

11
1 2 a
1 3 b
1 2 a
1 1 b
2 2
1 3 a
1 2 b
2 6
2 5
1 7 a
1 5 c

1
1
4
7
1
6
13
6
1
14
14

Hint

Sample Explanation

Operation Current string Answer
11 aa 0+1=10+1=1
22 aabbb 0+1+0+0+0=10+1+0+0+0=1
33 aabbbaa 0+1+0+0+0+1+2=40+1+0+0+0+1+2=4
44 aabbbaab 0+1+0+0+0+1+2+3=70+1+0+0+0+1+2+3=7
55 aabbb 0+1+0+0+0=10+1+0+0+0=1
66 aabbbaaa 0+1+0+0+0+1+2+2=60+1+0+0+0+1+2+2=6
77 aabbbaaabb 0+1+0+0+0+1+2+2+3+4=130+1+0+0+0+1+2+2+3+4=13
88 aabbbaaa 0+1+0+0+0+1+2+2=60+1+0+0+0+1+2+2=6
99 aabbb 0+1+0+0+0=10+1+0+0+0=1
1010 aabbbaaaaaaa 0+1+0+0+0+1+2+2+2+2+2+2=140+1+0+0+0+1+2+2+2+2+2+2=14
1111 aabbbaaaaaaaccccc 0+1+0+0+0+1+2+2+2+2+2+2+0+0+0+0+0=140+1+0+0+0+1+2+2+2+2+2+2+0+0+0+0+0=14

Constraints

20%20\% of the testdata satisfies n300n\leq 300 and for each operation of type 11, x300x\leq 300.

Another 30%30\% of the testdata satisfies n105n\leq 10 ^ 5 and for each operation of type 11, x=1x=1.

Another 30%30\% of the testdata satisfies n105n\leq 10 ^ 5 and contains no operation of type 22.

100%100\% of the testdata satisfies n105n\leq 10 ^ 5 and for each operation of type 11, x104x\leq 10 ^ 4.

Translated by ChatGPT 5