QOJ.ac

QOJ

时间限制: 0.4 s 内存限制: 1024 MB 总分: 100

#8512. 调和运算

统计

在本题中,我们考虑三种可以应用于任何长度 $|t| \ge 2$ 的 0-索引字符串 $t$ 的操作:

  • $I(t)$:反转 $t$。
  • $R(t, D)$:将 $t$ 向右循环移动 $D$ 个位置,其中 $D$ 为正整数且 $D < |t|$。即对于每个 $0 \le i < |t|$,在 $R(t, D)$ 中位置 $(i + D) \pmod{|t|}$ 处的字符即为 $t$ 中位置 $i$ 处的字符。
  • $L(t, D)$:与 $R(t, D)$ 类似,但将 $t$ 向左循环移动而不是向右。

例如,$I(\text{"pda"}) = \text{"adp"}$,$R(\text{"pda"}, 2) = \text{"dap"}$,以及 $L(\text{"pda"}, 2) = \text{"apd"}$。注意,对于任何 $t$ 和任何 $D$,都有 $|I(t)| = |R(t, D)| = |L(t, D)| = |t|$。

当一系列上述操作应用于一个字符串时,它们会按照列表顺序依次执行。也就是说,列表中的第一个操作应用于原始字符串,第二个操作应用于执行完第一个操作后的结果,第三个操作应用于执行完前两个操作后的结果,依此类推。

给定一个由小写字母组成的字符串 $S$ 以及一个包含 $K$ 个操作的列表 $F_1, F_2, \dots, F_K$。你的任务是找出有多少对索引 $(i, j)$ 满足 $1 \le i \le j \le K$,使得将操作子序列 $F_i, F_{i+1}, \dots, F_j$ 应用于 $S$ 后,最终结果仍为 $S$。

例如,设 $S = \text{"pda"}$,$K = 2$,$F_1 = R(t, 2)$,$F_2 = L(t, 2)$。将子序列 $F_1$ 应用于 $S$ 的结果是 $R(\text{"pda"}, 2) = \text{"dap"}$,这与 $S$ 不同。将子序列 $F_1, F_2$ 应用于 $S$ 的结果是 $L(R(\text{"pda"}, 2), 2) = L(\text{"dap"}, 2) = \text{"pda"} = S$。最后,将子序列 $F_2$ 应用于 $S$ 的结果是 $L(\text{"pda"}, 2) = \text{"apd"}$,这与 $S$ 不同。因此,在这个例子中,答案为 1。

输入格式

第一行包含一个字符串 $S$ ($2 \le |S| \le 2 \cdot 10^5$),由小写字母组成。

第二行包含一个整数 $K$ ($1 \le K \le 2 \cdot 10^5$),表示所考虑的操作列表中的操作数量。

接下来的 $K$ 行描述了操作,按它们在列表中出现的顺序排列,每行一个操作。如果操作是 $I(t)$,则该行包含大写字母 “I”。如果操作是 $R(t, D)$,则该行包含大写字母 “R” 和整数 $D$ ($1 \le D < |S|$)。最后,如果操作是 $L(t, D)$,则该行包含大写字母 “L” 和整数 $D$ ($1 \le D < |S|$)。

输出格式

输出一行,包含一个整数,表示所求的对数。

样例

输入 1

pda
2
R 2
L 2

输出 1

1

输入 2

aaa
4
R 1
I
I
R 1

输出 2

10

输入 3

caso
6
L 1
I
I
R 1
I
I

输出 3

4

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.