cf Educational Codeforces Round 127 E. Preorder

原题:
E. Preorder
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

You are given a rooted tree of 2 n − 1 2^n−1 2n1 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 s x s_x 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 s x s_x sx;
otherwise, the preorder string of x is s x + f ( l x ) + f ( r x ) s_x+f(lx)+f(rx) sx+f(lx)+f(rx), where + operator defines concatenation of strings, f ( l x ) f(lx) f(lx) is the preorder string of the left child of x, and f ( r x ) f(rx) 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).

input

The first line contains one integer n
(2≤n≤18).

The second line contains a sequence of 2 n 2^n 2n−1
characters s1,s2,…, s 2 n − 1 s_{2^n}−1 s2n1

. Each character is either A or B. The characters are not separated by spaces or anything else.
Output

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.

Examples
Input

4
BAAAAAAAABBABAB

Output
Copy

16

Input

2
BAA

Output

1

Input

2
ABA

Output

2

Input

2
AAB

Output

2

Input

2
AAA

Output

1

中文:
给你一个完美的二叉树,每个二叉树上有个字符,要么是A要么是B,现在定义一个preorder string,给定一个节点v,它的preorder string就是将v的左孩子preorder string,节点v以及v的右孩子的preorder string 进行concat起来。
如果节点v是一个叶子节点,那么它的preorder string就是v本身。

现在让你可以随意交换一个节点的左右子树,问你root节点的preorder string有多少种可能,结果对998244353取模。

代码:

    #include<bits/stdc++.h>
    using namespace std;
     
    typedef long long ll;
    const ll N = 3e5;
    const ll mod = 998244353;
     
    int n, c[N], m;
    ll dp[N];
     
    string s[N], tmp;
     
     
    void dfs(int root)
    {
        int lc = root << 1;
        int rc = root << 1 | 1;
    	if(rc > m) {
            s[root] = c[root] + '0';
            dp[root] = 1;
            return;
        }
     
        // left child
    	dfs(lc);
     
        // right child
        dfs(rc);
     
        ll tmp = (dp[lc] * dp[rc]) % mod;
    	if(s[lc]==s[rc]) {
            dp[root] = tmp;
        }
    	else { 
            dp[root] = tmp * 2 % mod;
        }
    	s[root] = c[root] + '0';
    	if(s[lc] < s[rc]) {
            s[root] += s[lc] + s[rc];
        }
    	else {
            s[root] += s[rc] + s[lc];
        }
    }
     
    int main()
    {
        while(cin >> n) {
            cin >> tmp;
            m = ( 1 << n) - 1;
            memset(dp, 0, sizeof(dp));
            for(int i = 0; i < tmp.size(); i++)
            {
                if(tmp[i]=='A') {
                    c[i + 1] = 1;
                } else {
                    c[i + 1] = 0;
                }
            }
            dfs(1);
            cout << dp[1] << endl;
        }
        return 0;
    }

思路:

一个组合计数和二叉树的问题。考虑下面的图
在这里插入图片描述红色数字表示当前节点有几种preorder string,叶子节点的preorder string自然就是1.
lc和rc分别表示左右孩子节点,左图计算根节点的有多少中preorder string,是将lc的结果乘以rc的结果在乘以2,因为可以将根节点的左右子树进行交换。

右侧图的根节点仅将lc乘以rc,因为lc和rc完全相同。

因此,只要能够判断两个子树是否“相同”,就可以使用递推的方式,得到根节点的preoder string的个数。
将上面的图右侧的树最右的叶子节点换个值,看下面这个图:
在这里插入图片描述
此时的lc与rc仍然可以是看作“相同”的,因为将lc的左子树和右子树(两个绿色节点)调换一下,就和lc相同了。

所以这里的相同,可以定义成:如果一个子树可以经过递归的调换其左右子树,最后得到另外一个子树,那么就可以说这两个子树相同。

考虑根据上面的条件,将所有的子树全部都用换成一个字典序最小的表达,如果两个子树的字典序最小表达是相同的,那么这两个子树就是相同的。可以这样理解,将字母AB换成01,遍历当前子树,如果发现左右子树的01值不相同,那么就将0值的子树换到左边,1值的子树换到右边。这样的统一表达方式,可以构造一个唯一的表达形式。

在代码中对每个节点维护一个字符串,用来记录最小表达。同事后序遍历二叉树,最后递推到根节点即可。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值