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
    评论
"educational codeforces round 103 (rated for div. 2)"是一个Codeforces平台上的教育性比赛,专为2级选手设计评级。以下是有关该比赛的回答。 "educational codeforces round 103 (rated for div. 2)"是一场Codeforces平台上的教育性比赛。Codeforces是一个为程序员提供竞赛和评级的在线平台。这场比赛是专为2级选手设计的,这意味着它适合那些在算法数据结构方面已经积累了一定经验的选手参与。 与其他Codeforces比赛一样,这场比赛将由多个问题组成,选手需要根据给定的问题描述和测试用例,编写程序来解决这些问题。比赛的时限通常有两到三个小时,选手需要在规定的时间内提交他们的解答。他们的程序将在Codeforces的在线评测系统上运行,并根据程序的正确性和效率进行评分。 该比赛被称为"educational",意味着比赛的目的是教育性的,而不是针对专业的竞争性。这种教育性比赛为选手提供了一个学习和提高他们编程技能的机会。即使选手没有在比赛中获得很高的排名,他们也可以从其他选手的解决方案中学习,并通过参与讨论获得更多的知识。 参加"educational codeforces round 103 (rated for div. 2)"对于2级选手来说是很有意义的。他们可以通过解决难度适中的问题来测试和巩固他们的算法和编程技巧。另外,这种比赛对于提高解决问题能力,锻炼思维和提高团队合作能力也是非常有帮助的。 总的来说,"educational codeforces round 103 (rated for div. 2)"是一场为2级选手设计的教育性比赛,旨在提高他们的编程技能和算法能力。参与这样的比赛可以为选手提供学习和进步的机会,同时也促进了编程社区的交流与合作。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值