题意:

给出一个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. 数学(组合,容斥):COGS 1220. 盒子与球

    1220. 盒子与球 ★   输入文件:boxball.in   输出文件:boxball.out   简单对比 时间限制:1 s   内存限制:128 MB [问题描述] 现有r个互不相同的盒子和n ...

  2. call, apply && bind, currying

    简要概括: apply(): 将函数作为指定对象的方法来调用,传递给它的是指定的参数数组 ——function.apply(thisobj, args) 或者 function.apply(thiso ...

  3. 我家用的网络IP地址给定,MAC绑定,我买了个无线路由器,请问怎么设定能让我的电脑和手机都能上网

    我家用的网络IP地址给定,MAC绑定,我买了个无线路由器,请问怎么设定能让我的电脑和手机都能上网   房东给的IP地址是:192.168.1.5 255.255.255.0 192.168.1.1 2 ...

  4. Velocity常用方法

    1.字符串替换 replace#if($!{name} != '')#set($tempName = $!{name})#set($tempName = $tempName.Replace('abc' ...

  5. 使用jquery trigger 触发a标签的click事件取代window.open方法

    var ohtml='<div class="friend-dialog tac pt15 pb20">'+ '<div class="f-h32&qu ...

  6. 10个经典的Java面试题

    这里有10个经典的Java面试题,也为大家列出了答案.这是Java开发人员面试经常容易遇到的问题,相信你了解和掌握之后一定会有所提高.让我们一起来看看吧. 1.Java的HashMap是如何工作的? ...

  7. vue-cli 中 使用vue-resource 输出后台数据

    阅读此文前,请了解vue-cli 组件如何使用  http://www.cnblogs.com/pearl07/p/6252116.html 1,mock(模拟)后台数据(新建data.Json文件) ...

  8. dubbo简述

    转http://blog.csdn.net/hzzhoushaoyu/article/details/4327309 一.前言 部门去年年中开始各种改造,第一步是模块服务化,这边初选dubbo试用在一 ...

  9. 博客SEO-搜索引擎工作原理简介

    资源推荐 Zac出版的<SEO实战密码>是SEO入门的好书,可惜我在当当网买的电子书受DRM版权保护,无法与大家分享. 我在网上找到了此书的  了解搜索引擎  章节,非常详细,且容易理解. ...

  10. java中的String.format使用

         format(String  format, Objece...  argues)函数相当于C语言中的printf函数,但是相对来说更灵活.      和C中的printf函数差不多,在fo ...