九度oj 1044
2015-08-16 14:18
176 查看
题目描述:
We are all familiar with pre-order, in-order and post-order traversals of binary trees. A common problem in data structure classes is to find the pre-order traversal of a binary tree when given the in-order and post-order traversals. Alternatively,
you can find the post-order traversal when given the in-order and pre-order. However, in general you cannot determine the in-order traversal of a tree when given its pre-order and post-order traversals. Consider the four binary trees below:
All of these trees have the same pre-order and post-order traversals. This phenomenon is not restricted to binary trees, but holds for general m-ary trees as well.
输入:
Input will consist of multiple problem instances. Each instance will consist of a line of the form
m s1 s2
indicating that the trees are m-ary trees, s1 is the pre-order traversal and s2 is the post-order traversal.All traversal strings will consist of lowercase alphabetic characters. For all input instances, 1 <= m <= 20 and the length of s1 and s2 will
be between 1 and 26 inclusive. If the length of s1 is k (which is the same as the length of s2, of course), the first k letters of the alphabet will be used in the strings. An input line of 0 will terminate the input.
输出:
For each problem instance, you should output one line containing the number of possible trees which would result in the pre-order and post-order traversals for the instance. All output values will be within the range of
a 32-bit signed integer. For each problem instance, you are guaranteed that there is at least one tree with the given pre-order and post-order traversals.
样例输入:
样例输出:
来源:
2008年上海交通大学计算机研究生机试真题
#include <stdio.h>
#include <string>
#include <iostream>
using namespace std;
int c[21][21];
int n;
long long test(string pre, string post) {
long long sum = 1;
int num = 0;
int k = 0, i;
pre.erase(pre.begin());
post=post.substr(0, post.length()-1);
while (k < pre.length()) {
for (i = 0; i < post.length(); i++)
if (pre[k] == post[i]) {
sum *= test(pre.substr(k, i - k + 1),
post.substr(k, i - k + 1));
num++; //num代表串被分成了几段(例如 (bejkcfghid,jkebfghicd) = (bejk, cfghi, d) )
k = i + 1;
break;
}
}
//cout << pre << " " << post <<" " << t1 << " =" << num << endl << endl;
sum *= c[num]
; //从n中取num个的取法个数
return sum;
}
void getsc() {
int i, j;
c[0][1] = c[1][1] = 1;
for (i = 2; i < 21; i++) {
c[0][i] = 1;
for (j = 1; j <= i; j++){
if (i == j)
c[j][i] = 1;
else
c[j][i] = c[j - 1][i - 1] + c[j][i - 1];
}
}
}
int main() {
string pre, post;
getsc();
while ((cin >> n >> pre >> post) && n) {
cout << test(pre, post) << endl; //printf("%ld\n",test(pre,post));
}
return 0;
}
We are all familiar with pre-order, in-order and post-order traversals of binary trees. A common problem in data structure classes is to find the pre-order traversal of a binary tree when given the in-order and post-order traversals. Alternatively,
you can find the post-order traversal when given the in-order and pre-order. However, in general you cannot determine the in-order traversal of a tree when given its pre-order and post-order traversals. Consider the four binary trees below:
All of these trees have the same pre-order and post-order traversals. This phenomenon is not restricted to binary trees, but holds for general m-ary trees as well.
输入:
Input will consist of multiple problem instances. Each instance will consist of a line of the form
m s1 s2
indicating that the trees are m-ary trees, s1 is the pre-order traversal and s2 is the post-order traversal.All traversal strings will consist of lowercase alphabetic characters. For all input instances, 1 <= m <= 20 and the length of s1 and s2 will
be between 1 and 26 inclusive. If the length of s1 is k (which is the same as the length of s2, of course), the first k letters of the alphabet will be used in the strings. An input line of 0 will terminate the input.
输出:
For each problem instance, you should output one line containing the number of possible trees which would result in the pre-order and post-order traversals for the instance. All output values will be within the range of
a 32-bit signed integer. For each problem instance, you are guaranteed that there is at least one tree with the given pre-order and post-order traversals.
样例输入:
2 abc cba 2 abc bca 10 abc bca 13 abejkcfghid jkebfghicda
样例输出:
4 1 45 207352860
来源:
2008年上海交通大学计算机研究生机试真题
#include <stdio.h>
#include <string>
#include <iostream>
using namespace std;
int c[21][21];
int n;
long long test(string pre, string post) {
long long sum = 1;
int num = 0;
int k = 0, i;
pre.erase(pre.begin());
post=post.substr(0, post.length()-1);
while (k < pre.length()) {
for (i = 0; i < post.length(); i++)
if (pre[k] == post[i]) {
sum *= test(pre.substr(k, i - k + 1),
post.substr(k, i - k + 1));
num++; //num代表串被分成了几段(例如 (bejkcfghid,jkebfghicd) = (bejk, cfghi, d) )
k = i + 1;
break;
}
}
//cout << pre << " " << post <<" " << t1 << " =" << num << endl << endl;
sum *= c[num]
; //从n中取num个的取法个数
return sum;
}
void getsc() {
int i, j;
c[0][1] = c[1][1] = 1;
for (i = 2; i < 21; i++) {
c[0][i] = 1;
for (j = 1; j <= i; j++){
if (i == j)
c[j][i] = 1;
else
c[j][i] = c[j - 1][i - 1] + c[j][i - 1];
}
}
}
int main() {
string pre, post;
getsc();
while ((cin >> n >> pre >> post) && n) {
cout << test(pre, post) << endl; //printf("%ld\n",test(pre,post));
}
return 0;
}
相关文章推荐
- JVM 堆内存设置原理
- 解决Maven提示:Could not read settings.xml, assuming default values
- py 格式转换为 exe 使用方法
- I/O复用--《APUE1》
- leetcode 的shell部分4道题整理
- Java集合---Map
- lca集锦
- URAL1306 Sequence Median(卡内存神题)
- Python包管理工具-pip
- Binary Tree Paths
- POJ 3904 Sky Code (容斥+莫比乌斯反演)
- 图像处理算法回顾基本框架
- NUBT 1580 素数分解 母函数法解
- openstack中虚拟机怎么与物理机通信
- notify vs nofifyall
- SSH深度历险记(九) Struts2+DWZ+Uploadify多文件(文件和图片等。)上传
- oc对象方法
- svn网络服务器项目导入到Eclipse
- win 环境安装python 和 Py pip
- 8个DBA最常用的监控Oracle数据库的常用shell脚本