题意:

给出一个m叉树的前,后序遍历求这样的树有多少种。


Solution:

我们知道前序遍历的第一个点一定是根节点,后序遍历的最后一个点一定是根节点。

由此,我们只一要确定对于每一个节点,它有多少个儿子节点,再累乘C(m,k)。

code

#include  <iostream>
#include <algorithm>
#include <string>
using namespace std;
string sq, sh;
int len, ans,m;
int Combination (int n, int m)
{
int ans = 1;
for (int i = 1; i <= m; i++)
ans = ans * (n - i + 1) / i;
return ans;
}
void make (int l, int r, int t, int w) {
if (l > r || t > w) return;
int p = 0, i = 0;
while (l <= r) {
char s = sq[l];
for (i = 0; i < len; i++) if (sh[i] == s) break;
int k = w - i+1;
make (l+1, l+k-1, i+1, w);
l = l + k;
w=i-1;
p++;
}
ans*=Combination(m,p);
}
int main() {
while (cin >>m>> sq >> sh) {
len = (int) sq.size() - 1;
ans=1;
reverse (sh.begin(), sh.end() );
make (1, len, 1, len);
cout<<ans<<endl;
}
return 0;
}

  

POJ 1240 Pre-Post-erous! 解题报告的更多相关文章

  1. POJ 2054 Color a Tree解题报告

    题干 Bob is very interested in the data structure of a tree. A tree is a directed graph in which a spe ...

  2. 【原创】POJ 1703 && RQNOJ 能量项链解题报告

    唉 不想说什么了 poj 1703,从看完题到写完第一个版本的代码,只有15分钟 然后一直从晚上八点WA到第二天早上 最后终于发现了BUG,题目要求的“Not sure yet.”,我打成了“No s ...

  3. poj 3750 小孩报数问题 解题报告

    题目链接:http://poj.org/problem?id=3750 约瑟夫问题,直接模拟即可. #include <iostream> #include <string> ...

  4. poj 3617 Best Cow Line 解题报告

    题目链接:http://poj.org/problem?id=3617 题目意思:给出一条长度为n的字符串S,目标是要构造一条字典序尽量小,长度为n的字符串T.构造的规则是,如果S的头部的字母 < ...

  5. poj 2771 Guardian of Decency 解题报告

    题目链接:http://poj.org/problem?id=2771 题目意思:有一个保守的老师要带他的学生来一次短途旅行,但是他又害怕有些人会变成情侣关系,于是就想出了一个方法: 1.身高差距   ...

  6. poj 1274 The Perfect Stall 解题报告

    题目链接:http://poj.org/problem?id=1274 题目意思:有 n 头牛,m个stall,每头牛有它钟爱的一些stall,也就是几头牛有可能会钟爱同一个stall,问牛与 sta ...

  7. [poj 3349] Snowflake Snow Snowflakes 解题报告 (hash表)

    题目链接:http://poj.org/problem?id=3349 Description You may have heard that no two snowflakes are alike. ...

  8. [poj 2480] Longge's problem 解题报告 (欧拉函数)

    题目链接:http://poj.org/problem?id=2480 题目大意: 题解: 我一直很欣赏数学题完美的复杂度 #include<cstring> #include<al ...

  9. Poj 1163 The Triangle 之解题报告

    Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 42232   Accepted: 25527 Description 7 3 ...

  10. POJ 1062 昂贵的聘礼 解题报告

    本题不难,但是笔者贡献了30多次Submit……就像Discuss讨论的一样,细节决定成败,WA了肯定有理由. 贴代码,Dijkstra+优先队列. #include <cstdio> # ...

随机推荐

  1. 牛逼的bootcss之buttons

    css源码 /*! @license * * Buttons * Copyright 2012-2014 Alex Wolfe and Rob Levin * * Licensed under the ...

  2. 高效算法——C 分饼

    My birthday is coming up and traditionally I’m serving pie. Not just one pie, no, I have a number N ...

  3. How to distribute your own Android library through jCenter and Maven Central from Android Studio

    In Android Studio, if you wish to include any library to your application. You could just simply add ...

  4. zoj 1586

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1586 //zoj 1586 #include<iostream> ...

  5. android下隐藏标题栏

    最近在开发android,遇到一个问题标题栏影响美观,查了很多网上方法. 用到了方法一: requestWindowFeature(Window.FEATURE_NO_TITLE); 虽然一直强调要在 ...

  6. 病毒侵袭 - HDU 2896(AC自动机)

    分析:有点需要注意的,输入的字符是所有可见的ASCII码,刚开始没看清一直以为是小写字母.............注意到这点后这题就是裸的自动机了.   代码如下: ================= ...

  7. HashTable和HashMap的区别

    1.HashTable线程安全,同步,效率相对低下. HashMap线程不安全,非同步,效率相对高 2.父类:HashTable的父类是Dictionary HashMap是AbstractMap 3 ...

  8. Android中的RelativeLayout

    安卓布局之一,RelativeLayout.又称之为相对布局.对于一个界面每个人都有不同的实现.我比较喜欢使用RelativeLayou.原因是,相对布局不会出现过多的嵌套,在现在硬件不断发展的今天, ...

  9. IE chrome兼容问题

    1.关于display显示和隐藏问题 document.getElementById("id").style.display="";//表示显示 documen ...

  10. TCP/UDP 、HTTP、IP 、socket 的关系。

    网络有上下分为7 层.物理层,数据链路层.网络层.会话层.应用层.传输层: IP协议位于网络层,IP和端口来控制网络流向: TCP.UDP是基于传输层.TCP保证三次握手.传递数据: UDP为不考虑是 ...