1 条题解

  • 0
    @ 2024-2-6 15:30:21

    简单分析可以发现,中序遍历不唯一的点必然是只有一个子节点的点,因为这样的点的子节点在左或在有都不影响前序和后序遍历的结果。这种点对应在前序和后序遍历中即形如 ABABBABA的段。根据乘法原理,答案就是这种2点的个数2^{点的个数}O(n2)O(n^2) 暴力枚举即可。

    #include <iostream>
    #include <string>
    
    using namespace std;
    
    string preorder, postorder;
    int ans = 1;
    
    int main() {
    	ios::sync_with_stdio(false);
    	cin.tie(nullptr), cout.tie(nullptr);
    
    	cin >> preorder >> postorder;
    	for (int i = 0; i < preorder.size() - 1; ++i)
    		for (int j = preorder.size() - 1; j > 0; --j)
    			if (preorder[i] == postorder[j] && preorder[i + 1] == postorder[j - 1])
    				ans <<= 1;
    	cout << ans << endl;
    
    	return 0;
    }
    
    • 1

    信息

    ID
    230
    时间
    1000ms
    内存
    125MiB
    难度
    3
    标签
    递交数
    7
    已通过
    7
    上传者