#P9642. [SNCPC2019] 0689

    ID: 8939 Type: RemoteJudge 2000ms 256MiB Tried: 0 Accepted: 0 Difficulty: 3 Uploaded By: Tags>2019O2优化陕西前缀和XCPC

[SNCPC2019] 0689

题目描述

We call a string as a 0689-string if this string only consists of digits 0, 6, 8 and 9. Given a 0689-string ss of length nn, one must\textbf{must} do the following operation exactly once: select a non-empty substring of ss and rotate it 180 degrees.

More formally, let sis_i be the ii-th character in string ss. After rotating the substring starting from sls_l and ending at srs_r 180 degrees (1lrn1 \le l \le r \le n), string ss will become string tt of length nn extracted from the following equation, where tit_i indicates the ii-th character in string tt:

$$t_i = \begin{cases} s_i & \text{if } 1 \le i < l \text{ or } r < i \le n \\ \text{`0'} & \text{if } l \le i \le r \text{ and } s_{l+r-i} = \text{`0'} \\ \text{`6'} & \text{if } l \le i \le r \text{ and } s_{l+r-i} = \text{`9'} \\ \text{`8'} & \text{if } l \le i \le r \text{ and } s_{l+r-i} = \text{`8'} \\ \text{`9'} & \text{if } l \le i \le r \text{ and } s_{l+r-i} = \text{`6'} \\ \end{cases}$$

What's the number of different strings one can get after the operation?

输入格式

There are multiple test cases. The first line of the input contains an integer TT, indicating the number of test cases. For each test case:

The first and only line contains a 0689-string ss (1s1061 \le |s| \le 10^6).

It's guaranteed that the sum of s|s| of all test cases will not exceed 10710^7.

输出格式

For each test case output one line containing one integer, indicating the number of different strings one can get after applying the operation exactly once.

题目大意

【题目描述】

我们称一个字符串为 0689-字符串,如果这个字符串只包含数字 0、6、8 和 9。给定一个长度为 nn 的 0689-字符串 ss,必须执行以下操作一次:选择 ss 的一个非空子串并将其旋转 180 度。

更正式地说,设 sis_i 为字符串 ss 的第 ii 个字符。在将从 sls_l 开始到 srs_r 结束的子串旋转180度后 (1lrn1 \le l \le r \le n),字符串 ss 将变成长度为 nn 的字符串 tt,其通过以下公式得到,其中 tit_i 表示字符串 tt 中的第 ii 个字符:

$$t_i = \begin{cases} s_i & \text{if } 1 \le i < l \text{ or } r < i \le n \\ \text{`0'} & \text{if } l \le i \le r \text{ and } s_{l+r-i} = \text{`0'} \\ \text{`6'} & \text{if } l \le i \le r \text{ and } s_{l+r-i} = \text{`9'} \\ \text{`8'} & \text{if } l \le i \le r \text{ and } s_{l+r-i} = \text{`8'} \\ \text{`9'} & \text{if } l \le i \le r \text{ and } s_{l+r-i} = \text{`6'} \\ \end{cases}$$

经过这个操作后,可以得到多少个不同的字符串?

【输入格式】

有多个测试用例。输入的第一行包含一个整数 TT,表示测试用例的数量。对于每个测试用例:

  • 第一行且唯一一行包含一个 0689-字符串 ss (1s1061 \le |s| \le 10^6)。 保证所有测试用例的 s|s| 之和不超过 10710^7

【输出格式】

对于每个测试用例输出一行,包含一个整数,表示经过一次操作可以得到的不同字符串的数量。

【样例解释】

我们在此解释第一个样例测试用例。

$$\begin{array}{|c|c||c|c|}\hline \textbf{子串} & \textbf{结果} & \textbf{子串} & \textbf{结果} \\ \hline 0 & 0689 & 68 & 0899 \\ \hline 6 & 0989 & 89 & 0668 \\ \hline 8 & 0689 & 068 & 8909 \\ \hline 9 & 0686 & 689 & 0689 \\ \hline 06 & 9089 & 0689 & 6890 \\ \hline \end{array} $$

很容易发现,经过这个操作可以得到 8 个不同的字符串。

翻译来自于:ChatGPT

2
0689
08
8
2

提示

We hereby explain the first sample test case.

$$\begin{array}{|c|c||c|c|}\hline \textbf{Substring} & \textbf{Result} & \textbf{Substring} & \textbf{Result} \\ \hline 0 & 0689 & 68 & 0899 \\ \hline 6 & 0989 & 89 & 0668 \\ \hline 8 & 0689 & 068 & 8909 \\ \hline 9 & 0686 & 689 & 0689 \\ \hline 06 & 9089 & 0689 & 6890 \\ \hline \end{array} $$

It's easy to discover that we can get 88 different strings after the operation.