九度 oj 题目1044:Pre-Post (确定树的状态)

来源:互联网 发布:淘宝商品价格怎么设置 编辑:程序博客网 时间:2024/05/04 16:06

http://ac.jobdu.com/problem.php?pid=1044

参考了

1.http://blog.csdn.net/yangnanhai93/article/details/40658571

2.http://www.acmerblog.com/jiudu-1044-2225.html


#include <stdio.h>#include <cstring>static int C[22][22];void getC(){     C[0][1] = C[1][1] = 1;    for (int i = 2; i <=20; ++i) {         C[0][i] = 1;        for (int j = 1;  j<=i; ++j) {             C[j][i] = C[j-1][i-1] + C[j][i-1];         }     }  } int solve(char *pre, char* post,int m){     int len = (int) strlen(pre);      if(len == 1) return 1; // only a root, status is unique     pre = pre + 1;  // remove the root    post[len-1] = '\0';    //find sub-tree    int count = 0; //number of sub-tree    int res = 1;   //number of tree-status     while(*pre){         int i=0;        char newPre[27],newPost[27];         while(pre[0]!=post[i]){             newPre[i] = pre[i];            newPost[i] = post[i];             i++;        }          newPre[i] = pre[i];        newPost[i] = post[i];        newPre[i+1] = '\0';        newPost[i+1] = '\0';        count++;         pre = pre+i+1;        post = post+i+1;        res = res*solve(newPre,newPost,m);     }      res = res*C[count][m];    return res;}  int main(){     //freopen("in/1044.in","r",stdin);     int m;     char pre[27],post[27];    getC();    while(scanf("%d %s %s",&m,pre,post) !=EOF){         printf("%d\n",solve(pre,post,m));     }  }  


0 0
原创粉丝点击