#P15122. [ICPC 2024 LAC] Harmonic Operations

[ICPC 2024 LAC] Harmonic Operations

题目描述

In this problem we consider three types of operations that can be applied to any 0-based string tt of length t2|t| \ge 2.

  • I(t)I(t): Reverses tt.
  • R(t,D)R(t, D): Rotates tt to the right DD positions, for some positive integer D<tD < |t|. That is, for each 0i<t0 \le i < |t|, the character at position (i+D)modt(i + D) \bmod |t| in R(t,D)R(t, D) is the character at position ii in tt.
  • L(t,D)L(t, D): Analogous to R(t,D)R(t, D), but rotates tt to the left instead of to the right.

For example, I(“pda”)=“adp”I(\text{“pda”}) = \text{“adp”}, R(“pda”,2)=“dap”R(\text{“pda”}, 2) = \text{“dap”}, and L(“pda”,2)=“apd”L(\text{“pda”}, 2) = \text{“apd”}. Note that for any tt and any DD it holds that I(t)=R(t,D)=L(t,D)=t|I(t)| = |R(t, D)| = |L(t, D)| = |t|.

When a list of the above operations is applied to a string, it is done sequentially in list order. That is, the first operation of the list is applied to the original string, the second operation is applied to the result after having applied the first operation, the third operation is applied to the result after having applied the first two operations, and so on.

You are given a string SS consisting of lowercase letters, and a list of KK operations F1,F2,,FKF_1, F_2, \dots, F_K. Your task is to find out how many pairs of indices (i,j)(i, j) there are such that 1ijK1 \le i \le j \le K, and applying the sublist of operations Fi,Fi+1,,FjF_i, F_{i+1}, \dots, F_j to SS yields SS as the final result.

Consider for instance S=“pda”S = \text{“pda”}, K=2K = 2, F1=R(t,2)F_1 = R(t, 2) and F2=L(t,2)F_2 = L(t, 2). The result of applying the sublist F1F_1 to SS is R(“pda”,2)=“dap”R(\text{“pda”}, 2) = \text{“dap”}, which is different from SS. The result of applying the sublist F1,F2F_1, F_2 to SS is $L(R(\text{“pda”}, 2), 2) = L(\text{“dap”}, 2) = \text{“pda”} = S$. Finally, the result of applying the sublist F2F_2 to SS is L(“pda”,2)=“apd”L(\text{“pda”}, 2) = \text{“apd”}, which is different from SS. Thus, in this example the answer is 1.

输入格式

The first line contains a string SS (2S21052 \le |S| \le 2 \cdot 10^5) which is made up of lowercase letters.

The second line contains an integer KK (1K21051 \le K \le 2 \cdot 10^5) indicating the number of operations in the list of operations that is being considered.

Operations are described in the next KK lines, in the order they appear in the list, one operation per line. If the operation is I(t)I(t), the line contains the uppercase letter “I”. If the operation is R(t,D)R(t, D), the line contains the uppercase letter “R” and the integer DD (1D<S1 \le D < |S|). Finally, if the operation is L(t,D)L(t, D), the line contains the uppercase letter “L” and the integer DD (1D<S1 \le D < |S|).

输出格式

Output a single line with an integer indicating the requested number of pairs.

pda
2
R 2
L 2
1
aaa
4
R 1
I
I
R 1
10
caso
6
L 1
I
I
R 1
I
I
4