USACO 3.4 American Heritage
American Heritage
Farmer John takes the heritage of his cows very seriously. He is not, however, a truly fine bookkeeper. He keeps his cow genealogies as binary trees and, instead of writing them in graphic form, he records them in the more linear `tree in-order' and `tree pre-order' notations.
Your job is to create the `tree post-order' notation of a cow's heritage after being given the in-order and pre-order notations. Each cow name is encoded as a unique letter. (You may already know that you can frequently reconstruct a tree from any two of the ordered traversals.) Obviously, the trees will have no more than 26 nodes.
Here is a graphical representation of the tree used in the sample input and output:
C
/ \
/ \
B G
/ \ /
A D H
/ \
E F
The in-order traversal of this tree prints the left sub-tree, the root, and the right sub-tree.
The pre-order traversal of this tree prints the root, the left sub-tree, and the right sub-tree.
The post-order traversal of this tree print the left sub-tree, the right sub-tree, and the root.
PROGRAM NAME: heritage
INPUT FORMAT
Line 1: | The in-order representation of a tree. |
Line 2: | The pre-order representation of that same tree. |
SAMPLE INPUT (file heritage.in)
ABEDFCHG
CBADEFGH
OUTPUT FORMAT
A single line with the post-order representation of the tree.
SAMPLE OUTPUT (file heritage.out)
AEFDBHGC —————————————————————
用前序遍历中某个字母的位置可以得到它的左子树的长度
然后就可以了
存代码
/*
ID: ivorysi
PROG: heritage
LANG: C++
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <set>
#include <vector>
#include <string.h>
#define siji(i,x,y) for(int i=(x);i<=(y);++i)
#define gongzi(j,x,y) for(int j=(x);j>=(y);--j)
#define xiaosiji(i,x,y) for(int i=(x);i<(y);++i)
#define sigongzi(j,x,y) for(int j=(x);j>(y);--j)
#define inf 0x3f3f3f3f
#define MAXN 400005
#define ivorysi
#define mo 97797977
#define ha 974711
#define ba 47
#define fi first
#define se second
#define pii pair<int,int>
using namespace std;
typedef long long ll;
int id[];
int root,lson[],rson[],fa[];
string str;
int dfs(string s,int fa) {
if(s.length()<) return ;
int u=s[]-'A'+;
if(s.length()==) {return u;}
lson[u]=dfs(s.substr(,id[u]-id[fa]-),fa);
rson[u]=dfs(s.substr(id[u]-id[fa]),u);
return u;
}
void ans(int u) {
if(u==) return;
ans(lson[u]);
ans(rson[u]);
printf("%c",u+'A'-);
}
void init() {
cin>>str;
xiaosiji(i,,str.length()) {
id[str[i]-'A'+]=i+;
}
cin>>str;
root=str[]-'A'+;
}
void solve() {
init();
dfs(str,);
ans(root);
puts("");
}
int main(int argc, char const *argv[])
{
#ifdef ivorysi
freopen("heritage.in","r",stdin);
freopen("heritage.out","w",stdout);
#else
freopen("f1.in","r",stdin);
#endif
solve();
}
USACO 3.4 American Heritage的更多相关文章
- 【洛谷P1827】【USACO】 美国血统 American Heritage 由二叉树两个序列求第三个序列
P1827 美国血统 American Heritage 题目描述 农夫约翰非常认真地对待他的奶牛们的血统.然而他不是一个真正优秀的记帐员.他把他的奶牛 们的家谱作成二叉树,并且把二叉树以更线性的&q ...
- 洛谷P1827 美国血统 American Heritage
P1827 美国血统 American Heritage 54通过 90提交 题目提供者JOHNKRAM 标签USACO 难度普及- 提交 讨论 题解 最新讨论 暂时没有讨论 题目描述 农夫约翰非 ...
- 洛谷 P1827 美国血统 American Heritage
P1827 美国血统 American Heritage 题目描述 农夫约翰非常认真地对待他的奶牛们的血统.然而他不是一个真正优秀的记帐员.他把他的奶牛 们的家谱作成二叉树,并且把二叉树以更线性的“树 ...
- 【美国血统 American Heritage 题解】已知前序中序 求后序
题目: 题目名称:美国血统 American Heritage 题目来源:美国血统 American Heritage ## 题目描述 农夫约翰非常认真地对待他的奶牛们的血统.然而他不是一个真正优秀的 ...
- American Heritage usaco
基础题,主要思路是找到根,然后分别递归即可: #include<iostream> #include<cstring> #include<string> #incl ...
- 洛谷 P1827 美国血统 American Heritage Label:字符串Water
题目描述 农夫约翰非常认真地对待他的奶牛们的血统.然而他不是一个真正优秀的记帐员.他把他的奶牛 们的家谱作成二叉树,并且把二叉树以更线性的“树的中序遍历”和“树的前序遍历”的符号加以记录而 不是用图形 ...
- USACO 完结的一些感想
其实日期没有那么近啦……只是我偶尔还点进去造成的,导致我没有每一章刷完的纪念日了 但是全刷完是今天啦 讲真,题很锻炼思维能力,USACO保持着一贯猎奇的题目描述,以及尽量不用高级算法就完成的题解……例 ...
- USACO Training刷题记录 By cellur925
Section 1.1 Your Ride Is Here 貌似没啥可说 Greedy Gift Givers 上来就想stl map映射,有两个坑:如果送给别人的人数为0,那么需要特判一下,防止整数 ...
- Home vs2013
Microsoft Visual Studio Ultimate 2013 版本 12.0.30110.00 Update 1 Microsoft .NET Framework 版本 4.5. ...
随机推荐
- SSMS2008插件开发(2)--Microsoft Visual Studio 2008插件开发介绍
原文:SSMS2008插件开发(2)--Microsoft Visual Studio 2008插件开发介绍 由于开发SSMS2008插件是通过VS2008进行的,有必要先介绍一下VS2008的插件开 ...
- Solr 教程
1.Solr安装 下载jdk-8u111-windows-i586_8.0.1110.14 下载solr-6.3.0.zip 2.配置JAVA_HOME 在"系统变量"中,设置3项 ...
- REST 测试工具
两款 REST 测试工具 用CURL命令行测试REST API 无疑是低效率的,这里把最近使用的两款 Chrome 插件总结下 POSTMAN 简单易用 REST Console 功能强大 使用的话用 ...
- linux 启动oracle报cannot restore segment prot after reloc: Permission denied
error while loading shared libraries: $ORACLE_HOME/lib/libnnz10.so: cannot restore segment prot afte ...
- [转]ANDROID 探究oom内幕
从早期G1的192MB RAM开始,到现在动辄1G -2G RAM的设备,为单个App分配的内存从16MB到48MB甚至更多,但OOM从不曾离我们远去.这是因为大部分App中图片内容占据了50%甚至7 ...
- 【分享】Python学习资源大合集
地址:http://www.hejizhan.com/html/xueke/520/x520_03.html Python安装软件合集(Windows)(78) Python教程——游戏编程(13) ...
- 登陆页面改为SSO验证
登陆页面改为SSO验证 单点登录(SSO,single sign-on)是一个会话或用户身份验证过程,用户只需要登录一次就可以访问所有相互信任的应用系统,二次登录时无需重新输入用户名和密码.简化账号登 ...
- 2014.3.6-C语言学习小结
链表基础: 知识点: 1.链表基础 2.节点的创建和添加 llist_append_node 3.链表的遍历 llist_print_each 4.链表的查找与修改 5.链表的插入与删除 6.链表的销 ...
- HTML页面规范分解
百度,淘宝,腾讯三大巨头HTML页面规范分解 [兼容html5方案] 百度贴吧,百度图片的实现 <!--[if lt IE 9]> <script> (function() ...
- Bootstrap3.0学习第三轮(栅格系统案例)
Bootstrap3.0学习第三轮(栅格系统案例) 前言 在前面的一篇文章当中http://www.cnblogs.com/aehyok/p/3400499.html主要学习了栅格系统的基本原理,以及 ...