CF2255B.A Ribbon for Tomorrow

普及/提高-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Nephren has never been fond of long farewells. Before Chtholly leaves for her next mission, she says nothing and begins preparing a small ribbon for her instead.

She places nn glass beads in a row on the table before threading them onto the ribbon. Each bead is either white or black. A binary∗^{\text{∗}} string ss represents their colors: the character 0\mathtt{0} represents a white bead, and the character 1\mathtt{1} represents a black bead.

To make the arrangement less ordinary, Nephren turns it into a small game. She can perform the following operation any number of times (possibly zero):

  • Choose two indices ll and rr (1≤l≤r≤n1 \le l \le r \le n) such that sl=srs_l=s_r in the current string, and reverse†^{\text{†}} the substring slsl+1…srs_l s_{l+1}\ldots s_r.

For example, if s=00110s=\mathtt{00110}, Nephren may choose l=1l=1 and r=5r=5, since s1=s5=0s_1=s_5=\mathtt{0}. After the operation, the string becomes 01100\mathtt{01100}.

Determine the number of different binary strings that can be obtained from ss. Since this number may be large, output it modulo 998 244 353998\,244\,353.

∗^{\text{∗}}A binary string is a string where each character is either 0\mathtt 0 or 1\mathtt 1.

†^{\text{†}}To reverse a substring slsl+1…srs_l s_{l+1}\ldots s_r means to replace it with srsr−1…sls_r s_{r-1}\ldots s_l.

涅芙莲向来不喜欢漫长的告别。在克洛伊出发执行下一次任务之前,她什么也没说,而是开始为她准备一条小小的丝带。

她先把 nn 颗玻璃珠排成一列放在桌上,再将它们穿到丝带上。每颗珠子非白即黑。一个二进制∗^{\text{∗}}字符串 ss 表示它们的颜色:字符 0\mathtt{0} 代表白色珠子,字符 1\mathtt{1} 代表黑色珠子。

为了让排列不那么寻常,涅芙莲将其变成一个小游戏。她可以执行以下操作任意多次(包括零次):

  • 选择两个下标 ll 和 rr(满足 1≤l≤r≤n1 \le l \le r \le n),使得当前字符串中 sl=srs_l = s_r,然后将子串 slsl+1…srs_l s_{l+1}\ldots s_r 翻转†^{\text{†}}。

例如,若 s=00110s = \mathtt{00110},涅芙莲可选取 l=1l = 1 和 r=5r = 5,因为 s1=s5=0s_1 = s_5 = \mathtt{0}。执行操作后,字符串变为 01100\mathtt{01100}。

求从 ss 出发能够得到的不同二进制字符串的总数。由于该数可能很大,请输出其对 998 244 353998\,244\,353 取模的结果。

∗^{\text{∗}} 二进制字符串是指每个字符均为 0\mathtt{0} 或 1\mathtt{1} 的字符串。

†^{\text{†}} 将子串 slsl+1…srs_l s_{l+1}\ldots s_r 翻转,是指将其替换为 srsr−1…sls_r s_{r-1}\ldots s_l。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1041 \le t \le 10^4). The description of the test cases follows.

The first line of each test case contains one integer nn (1≤n≤1061 \le n \le 10^6) — the number of beads.

The second line contains a binary string ss of length nn, describing the colors of the beads.

It is guaranteed that the sum of nn over all test cases does not exceed 10610^6.

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤1041 \le t \le 10^4)。随后是测试用例的描述。

每个测试用例的第一行包含一个整数 nn(1≤n≤1061 \le n \le 10^6)—— 表示珠子的数量。

第二行包含一个长度为 nn 的二进制字符串 ss,用于描述珠子的颜色。

保证所有测试用例的 nn 之和不超过 10610^6。

输出格式

For each test case, output a single integer — the number of different binary strings that can be obtained from ss, modulo 998 244 353998\,244\,353.

对于每个测试用例,输出一个整数——即可以从 ss 得到的不同二进制字符串的数目,对 998 244 353998\,244\,353 取模。

输入输出样例

  • 输入#1

    4
    5
    00110
    6
    001010
    5
    01010
    6
    111111

    输出#1

    2
    3
    1
    1

说明/提示

In the first test case, exactly the following two strings can be obtained:

  • 00110\mathtt{00110};
  • 01100\mathtt{01100}.

For example, reversing the entire string 00110\mathtt{00110} produces 01100\mathtt{01100}.

In the second test case, exactly the following three strings can be obtained:

  • 001010\mathtt{001010};
  • 010010\mathtt{010010};
  • 010100\mathtt{010100}.

For example, 010010\mathtt{010010} can be obtained by reversing the first four characters of 001010\mathtt{001010}, and 010100\mathtt{010100} can be obtained by reversing the entire string 001010\mathtt{001010}.

In the third test case, every substring whose endpoints contain the same character is a palindrome. Therefore, reversing any valid substring does not change the string, and only 01010\mathtt{01010} can be obtained.

In the fourth test case, we can only get 111111\mathtt{111111}.

在第一个测试用例中,恰好可以得到以下两个字符串:

  • 00110\mathtt{00110};
  • 01100\mathtt{01100}。

例如,将整个字符串 00110\mathtt{00110} 反转可得到 01100\mathtt{01100}。

在第二个测试用例中,恰好可以得到以下三个字符串:

  • 001010\mathtt{001010};
  • 010010\mathtt{010010};
  • 010100\mathtt{010100}。

例如,010010\mathtt{010010} 可通过对 001010\mathtt{001010} 的前四个字符进行反转得到;而 010100\mathtt{010100} 可通过对整个字符串 001010\mathtt{001010} 进行反转得到。

在第三个测试用例中,所有端点字符相同的子串均为回文串。因此,对任意合法子串进行反转均不会改变原字符串,故仅能得到 01010\mathtt{01010}。

在第四个测试用例中,我们只能得到 111111\mathtt{111111}。

输入解题思路,AI测评打分。不知道怎么写?

首页