zoukankan      html  css  js  c++  java
  • 1044.Pre-post

    题目描述:
        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

    # include<iostream>
    # include<string>
    using namespace std;
    int arr[21][21];
    //bool flag[21];
    string a1,a2;
    void initArr();
    //int Calcom(int m,int n);
    long long Cal(string s1,string s2,int m)
    {
        int i,j=0,n=0;
        long long sum=1;
        int len=s1.length();
        s1.erase(s1.begin());
        //cout<<s1;
        s2=s2.substr(0,s2.length()-1);
        //cout<<s2;
        while(s1.length()>j){    //a1.erase(0,len);
            for(i=j;i<s1.length();i++)
            {
                if(s1[j]==s2[i])
                    {
                        sum=sum*Cal(s1.substr(j,i-j+1),s2.substr(j,i-j+1),m);
                    j=i+1;
                    n++;
                    break;
                    }
             } 
           //
            //s1=s1.substr(i+1,len-1);
            //s2=s2.substr(i+1,len-1);
         
        }
        sum=sum*arr[n][m];
        return sum;
         
    }
    void initArr()
    {
        int i,j;
        arr[0][1]=arr[1][1]=1;
        for(i=2;i<21;i++)
        {
            arr[0][i]=1;
            for(j=1;j<=i;j++)
            {
                if(j==i)
                arr[j][i]=1;
                else arr[j][i]=arr[j-1][i-1]+arr[j][i-1];
            }
        }
         
    }
     
    int main()
    {
       int m;
       string  s1,s2;
       long long sum;
       while(cin>>m>>s1>>s2)
       {
          int len=s1.size();
             if (len==0)
            break;
          initArr();
            sum=Cal(s1,s2,m);
            cout<<sum<<endl;
        }
       return 0;
    } 
  • 相关阅读:
    【xsy1230】 树(tree) 点分治+线段树
    【xsy1237】 字符转换 矩阵快速幂
    【xsy1232】Magic 最小割
    【xsy1144】选物品 主席树
    【xsy3423】党² 线段树+李超线段树or动态半平面交
    $Django python中使用redis, django中使用(封装了),redis开启事务(管道)
    $Django redis内存数据库 (知识回顾cmd切换目录)
    $Django 路飞之课程下的分类,用户登陆成功前端存cookie,
    $Django 路飞之小知识回顾,Vue之样式element-ui,Vue绑定图片--mounted页面挂载--路由携带参数
    $Django 路飞学城项目简介
  • 原文地址:https://www.cnblogs.com/bernieloveslife/p/9736526.html
Copyright © 2011-2022 走看看