题意:

给出一个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. MySQL默认INFORMATION_SCHEMA,MySQL,TEST三个数据库用途

    本文简要说明了MySQL数据库安装好后自带的INFORMATION_SCHEMA,MySQL,TEST三个数据库的用途. 第一个数据库INFORMATION_SCHEMA:提供了访问数据库元数据的方式 ...

  2. appendGrid

    appendGrid appendGrid的使用

  3. -_-#【CSS3】CSS3 gradient transition with background-position

    CSS3 gradient transition with background-position <!DOCTYPE html> <html> <head> &l ...

  4. 解决weblogic Managed Server启动非常慢的情况

    jdk版本:1.7.0_79 查看控制台日志停留在如下地方: . . JAVA Memory arguments: -Xms2048m -Xmx4096m -XX:MaxPermSize=512m . ...

  5. sed删除文本第一个匹配行

    源文本如下,要求删除第一个为happy-123456的行. ----------------------------- aaaaaaa happy- bbbbbb asdasawe happy- ds ...

  6. 福州大学 Problem 2168 防守阵地 I

    http://acm.fzu.edu.cn/problem.php?pid=2168 最重要的是 dp[k]=dp[k-1]-ans[k-1]+x[i]*m; ans[k-1]是m个数求和.  Pro ...

  7. [Locked] Maximum Size Subarray Sum Equals k

    Example 1: Given nums = [1, -1, 5, -2, 3], k = 3,return 4. (because the subarray [1, -1, 5, -2] sums ...

  8. JEFF BANKS_百度百科

    JEFF BANKS_百度百科 JEFF BANKS

  9. sqlserver客户端连接只显示特定数据库的配置方法

    首先,在实例级,有一个 view any database的这个属性,打开时可以看到所有数据库的元数据表,因此能看到实例下所有数据库的名字.默认public角色拥有这个属性.所以新建的登陆是可以看到所 ...

  10. xargs 参数

    hadoop fs -ls /source/recommend/at_access | awk -F "/" '{print $NF}' | grep -v $(date +%Y% ...