Codeforces · 2255B
A Ribbon for Tomorrow
Count how many distinct binary strings are reachable by reversing substrings whose endpoints contain the same bit.
Problem
You are given a binary string s of length n. You may apply the following operation any number of times, including zero:
- choose two indices
landrsuch that1 ≤ l ≤ r ≤ nand, in the current string,s[l] = s[r]; - reverse the order of the characters in the substring
s[l..r].
Determine how many distinct binary strings can be obtained from s. Return the answer modulo 998244353.
Input
The first line contains the number of test cases t.
For each test case:
- one line contains the integer
n; - the next line contains a binary string
sof lengthn.
Output
For each test case, print one integer: the number of distinct binary strings reachable from s, modulo 998244353.
Constraints
1 ≤ t ≤ 10^4.1 ≤ n ≤ 10^6.scontains only0and1and has lengthn.- The sum of
nover all test cases does not exceed10^6. - Time limit: 2 seconds.
- Memory limit: 256 MB.
Notes
The condition s[l] = s[r] is evaluated on the current state of the string, after all previously applied operations.
Original source
Codeforces 2255B ↗