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;
} 
原文地址:https://www.cnblogs.com/bernieloveslife/p/9736526.html