CF1671E.Preorder
普及/提高-
通过率:0%
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are given a rooted tree of 2n−1 vertices. Every vertex of this tree has either 0 children, or 2 children. All leaves of this tree have the same distance from the root, and for every non-leaf vertex, one of its children is the left one, and the other child is the right one. Formally, you are given a perfect binary tree.
The vertices of the tree are numbered in the following order:
- the root has index 1 ;
- if a vertex has index x , then its left child has index 2x , and its right child has index 2x+1 .
Every vertex of the tree has a letter written on it, either A or B. Let's define the character on the vertex x as sx .
Let the preorder string of some vertex x be defined in the following way:
- if the vertex x is a leaf, then the preorder string of x be consisting of only one character sx ;
- otherwise, the preorder string of x is sx+f(lx)+f(rx) , where + operator defines concatenation of strings, f(lx) is the preorder string of the left child of x , and f(rx) is the preorder string of the right child of x .
The preorder string of the tree is the preorder string of its root.
Now, for the problem itself...
You have to calculate the number of different strings that can be obtained as the preorder string of the given tree, if you are allowed to perform the following operation any number of times before constructing the preorder string of the tree:
- choose any non-leaf vertex x , and swap its children (so, the left child becomes the right one, and vice versa).
输入格式
The first line contains one integer n ( 2≤n≤18 ).
The second line contains a sequence of 2n−1 characters s1,s2,…,s2n−1 . Each character is either A or B. The characters are not separated by spaces or anything else.
输出格式
Print one integer — the number of different strings that can be obtained as the preorder string of the given tree, if you can apply any number of operations described in the statement. Since it can be very large, print it modulo 998244353 .
输入输出样例
输入#1
4 BAAAAAAAABBABAB
输出#1
16
输入#2
2 BAA
输出#2
1
输入#3
2 ABA
输出#3
2
输入#4
2 AAB
输出#4
2
输入#5
2 AAA
输出#5
1