OJ_1044 Pre-Post

本文介绍了一种算法,该算法通过给定的前序和后序遍历序列来计算可能的二叉树中序遍历序列的数量。特别地,文章提供了详细的代码实现,并举例说明了如何通过递归方式分解问题,最终解决复杂实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <iostream>
#include <string>
using namespace std;
long long getcomb(int n,int m)
{
     long long a=1;
     long long b=1;
     for(int i=0;i<m;i++)
     {
             a*=n-i;
             b*=m-i;
     }
     return a/b;
}
long long getresult(string a,string b,int n){
     
       long long sum=1;
       if(a.size()==1)
                      return sum;
       else{
            int count=0;
            string sub1,sub2;
            a=a.substr(1);
            b=b.substr(0,b.size()-1);
            
            while(a.size()!=0)
            {
                 int pos=b.find(a[0]);
                 sub1=a.substr(0,pos+1);
                 sub2=b.substr(0,pos+1);
                 a=a.substr(pos+1);
                 b=b.substr(pos+1);
                 count++;
                 sum*=getresult(sub1,sub2,n);
            }
            return sum*getcomb(n,count);
       }
     
}
void func()
{
    int m;
    string s1,s2;
    while(cin>>m)
    {
                 if(m==0)break;
                 cin>>s1>>s2;
                 long long ret=getresult(s1,s2,m);
                 cout<<ret<<endl;
    }
   
}
int main(int argc, char *argv[])
{
    
	//printf("Hello, world\n");
	func();
	return 0;
}

给出前序后序,求中序情况个数

分析:

例如:

10 abc bca
根节点为a是确定的,接下来是 bc bc
可知b,c在同一级别,有C(10,2)=45 (10个位置中取两个)

2 abc cba
同样根节点为a,然后是 bc cb

b,c在两层 C(2,1) * C(2,1)=4


对于这个就有些复杂了,

13 abejkcfghid jkebfghicda
第一步拆分为 三部分 (bejk, cfghi, d) * C(13,3)

再继续递归下去,直到字符串长度为1



题目描述:

        We are all familiar with pre-order, in-order and post-order traversals of binary trees. A common problem in data structure classes is to find the pre-order traversal of a binary tree when given the in-order and post-order traversals. Alternatively, you can find the post-order traversal when given the in-order and pre-order. However, in general you cannot determine the in-order traversal of a tree when given its pre-order and post-order traversals. Consider the four binary trees below:



    All of these trees have the same pre-order and post-order traversals. This phenomenon is not restricted to binary trees, but holds for general m-ary trees as well. 

输入:

        Input will consist of multiple problem instances. Each instance will consist of a line of the form 
m s1 s2 
        indicating that the trees are m-ary trees, s1 is the pre-order traversal and s2 is the post-order traversal.All traversal strings will consist of lowercase alphabetic characters. For all input instances, 1 <= m <= 20 and the length of s1 and s2 will be between 1 and 26 inclusive. If the length of s1 is k (which is the same as the length of s2, of course), the first k letters of the alphabet will be used in the strings. An input line of 0 will terminate the input.

输出:
        For each problem instance, you should output one line containing the number of possible trees which would result in the pre-order and post-order traversals for the instance. All output values will be within the range of a 32-bit signed integer. For each problem instance, you are guaranteed that there is at least one tree with the given pre-order and post-order traversals. 

样例输入:
2 abc cba
2 abc bca
10 abc bca
13 abejkcfghid jkebfghicda
样例输出:
4
1
45
207352860

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值