POJ1240

Pre-Post-erous!
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 2374 Accepted: 1464

Description

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: 
    a   a   a   a

   /   /     \   \

  b   b       b   b

 /     \     /     \

c       c   c       c


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

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. 

Output

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. 

Sample Input

2 abc cba
2 abc bca
10 abc bca
13 abejkcfghid jkebfghicda
0

Sample Output

4
1
45

207352860

这个题也比较简单,写的开心

#include <iostream> #include <cstring> #include <stdio.h> using namespace std; int m,sum; char s1[25],s2[25]; int fd(int lpre,int rpre,int lpost,int rpost) {     if(lpre==rpre)         return 1;     int cou,nr[20],nl[20],i,j,k,ve,to;     k=1;     i=lpre+1;     j=lpost;     cou=0;     nr[0]=j-1;     nl[0]=i-1;     while(i<=rpre)     {         char tem=s1[i];         //cout<<i<<endl;         for(;j<=rpost-1;j++)         {             i++;             if(tem==s2[j])             {                 cou++;                 nl[k]=i-1;                 nr[k]=j;                 k++;                 j++;                 //cout<<"sdsa"<<endl;                 break;             }         }     }     ve=1;     for(i=0;i<cou;i++)         ve=ve*(m-i)/(i+1);     //cout<<cou<<endl<<ve<<endl;     to=1;     for(i=0;i<cou;i++){         //cout<<nl[i]+1<<"-"<<nl[i+1]<<endl;         //cout<<nr[i]+1<<"-"<<nr[i+1]<<endl;         to=to*fd(nl[i]+1,nl[i+1],nr[i]+1,nr[i+1]);     }     return to*ve; } int main() {     cin>>m>>s1>>s2;     int lpre,rpre,lpost,rpost,len;     while(m!=0)     {         len=strlen(s1);         lpre=0;rpre=len-1;         lpost=0;rpost=len-1;         cout<<fd(lpre,rpre,lpost,rpost)<<endl;         cin>>m>>s1>>s2;     }     return 0; }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值