POJ 1240 Pre-Post-erous! 解题报告
题意:
给出一个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! 解题报告的更多相关文章
- 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 ...
- 【原创】POJ 1703 && RQNOJ 能量项链解题报告
唉 不想说什么了 poj 1703,从看完题到写完第一个版本的代码,只有15分钟 然后一直从晚上八点WA到第二天早上 最后终于发现了BUG,题目要求的“Not sure yet.”,我打成了“No s ...
- poj 3750 小孩报数问题 解题报告
题目链接:http://poj.org/problem?id=3750 约瑟夫问题,直接模拟即可. #include <iostream> #include <string> ...
- poj 3617 Best Cow Line 解题报告
题目链接:http://poj.org/problem?id=3617 题目意思:给出一条长度为n的字符串S,目标是要构造一条字典序尽量小,长度为n的字符串T.构造的规则是,如果S的头部的字母 < ...
- poj 2771 Guardian of Decency 解题报告
题目链接:http://poj.org/problem?id=2771 题目意思:有一个保守的老师要带他的学生来一次短途旅行,但是他又害怕有些人会变成情侣关系,于是就想出了一个方法: 1.身高差距 ...
- poj 1274 The Perfect Stall 解题报告
题目链接:http://poj.org/problem?id=1274 题目意思:有 n 头牛,m个stall,每头牛有它钟爱的一些stall,也就是几头牛有可能会钟爱同一个stall,问牛与 sta ...
- [poj 3349] Snowflake Snow Snowflakes 解题报告 (hash表)
题目链接:http://poj.org/problem?id=3349 Description You may have heard that no two snowflakes are alike. ...
- [poj 2480] Longge's problem 解题报告 (欧拉函数)
题目链接:http://poj.org/problem?id=2480 题目大意: 题解: 我一直很欣赏数学题完美的复杂度 #include<cstring> #include<al ...
- Poj 1163 The Triangle 之解题报告
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 42232 Accepted: 25527 Description 7 3 ...
- POJ 1062 昂贵的聘礼 解题报告
本题不难,但是笔者贡献了30多次Submit……就像Discuss讨论的一样,细节决定成败,WA了肯定有理由. 贴代码,Dijkstra+优先队列. #include <cstdio> # ...
随机推荐
- Mac开发者必备实用工具推荐
最近一个师兄给我推荐了一些Mac上的实用工具,用起来非常顺手,能提高不少开发效率.于是就想着把自己之前用过的其他工具也整理一下,一块推荐给大家,希望能对大家有帮助. Alfred 目前Mac下最好用的 ...
- 【HDOJ】2699 Five in a Row
wa了几次,至少要考虑4个方向:下.右.左下.右下.很像当年北航的机试题目. /* 2699 */ #include <iostream> #include <cstdio> ...
- Linux Shell编程(4)——shell特殊字符(上)
在脚本或其他别的地方出现的特殊字符#注释. 以一个#开头的行 (#!是例外) 是注释行.# 这是一行注释.注释也可以出现在一个命令语句的后面.echo "A comment will fol ...
- CentOS 6.3上搭建PPTP VPN
系统版本:CentOS 6.3_x86_64 eth0:172.16.10.72(实验环境当公网IP使用) eth1:192.168.100.50 1.检测是否支持ppp模块 # cat /dev/p ...
- 【宽搜】BAPC2014 J Jury Jeopardy (Codeforces GYM 100526)
题目链接: http://codeforces.com/gym/100526 http://acm.hunnu.edu.cn/online/?action=problem&type=show& ...
- 数学概念——J - 数论,质因数分解
J - 数论,质因数分解 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit ...
- 用Processon在线绘制UML的尝试
地址https://www.processon.com/ ProcessOn是一个面向垂直专业领域的作图工具和社交网络,成立于2011年6月并于2012年启动.ProcessOn将全球的专家顾问.咨询 ...
- Keywords Search - HDU 2222(AC自动机模板)
题目大意:输入几个子串,然后输入一个母串,问在母串里面包含几个子串. 分析:刚学习的AC自动机,据说这是个最基础的模板题,所以也是用了最基本的写法来完成的,当然也借鉴了别人的代码思想,确实是个很神 ...
- Redis 实现用户积分排行榜
排行榜功能是一个很普遍的需求.使用 Redis 中有序集合的特性来实现排行榜是又好又快的选择. 一般排行榜都是有实效性的,比如“用户积分榜”.如果没有实效性一直按照总榜来排,可能榜首总是几个老用户,对 ...
- 自己去看dubbo源码
编译Dubbo源码并测试 2014.09.24 | Comments 转http://blog.javachen.com/2014/09/24/compile-and-test-dubbo.html ...