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的更多相关文章

  1. 【洛谷P1827】【USACO】 美国血统 American Heritage 由二叉树两个序列求第三个序列

    P1827 美国血统 American Heritage 题目描述 农夫约翰非常认真地对待他的奶牛们的血统.然而他不是一个真正优秀的记帐员.他把他的奶牛 们的家谱作成二叉树,并且把二叉树以更线性的&q ...

  2. 洛谷P1827 美国血统 American Heritage

    P1827 美国血统 American Heritage 54通过 90提交 题目提供者JOHNKRAM 标签USACO 难度普及- 提交  讨论  题解 最新讨论 暂时没有讨论 题目描述 农夫约翰非 ...

  3. 洛谷 P1827 美国血统 American Heritage

    P1827 美国血统 American Heritage 题目描述 农夫约翰非常认真地对待他的奶牛们的血统.然而他不是一个真正优秀的记帐员.他把他的奶牛 们的家谱作成二叉树,并且把二叉树以更线性的“树 ...

  4. 【美国血统 American Heritage 题解】已知前序中序 求后序

    题目: 题目名称:美国血统 American Heritage 题目来源:美国血统 American Heritage ## 题目描述 农夫约翰非常认真地对待他的奶牛们的血统.然而他不是一个真正优秀的 ...

  5. American Heritage usaco

    基础题,主要思路是找到根,然后分别递归即可: #include<iostream> #include<cstring> #include<string> #incl ...

  6. 洛谷 P1827 美国血统 American Heritage Label:字符串Water

    题目描述 农夫约翰非常认真地对待他的奶牛们的血统.然而他不是一个真正优秀的记帐员.他把他的奶牛 们的家谱作成二叉树,并且把二叉树以更线性的“树的中序遍历”和“树的前序遍历”的符号加以记录而 不是用图形 ...

  7. USACO 完结的一些感想

    其实日期没有那么近啦……只是我偶尔还点进去造成的,导致我没有每一章刷完的纪念日了 但是全刷完是今天啦 讲真,题很锻炼思维能力,USACO保持着一贯猎奇的题目描述,以及尽量不用高级算法就完成的题解……例 ...

  8. USACO Training刷题记录 By cellur925

    Section 1.1 Your Ride Is Here 貌似没啥可说 Greedy Gift Givers 上来就想stl map映射,有两个坑:如果送给别人的人数为0,那么需要特判一下,防止整数 ...

  9. Home vs2013

        Microsoft Visual Studio Ultimate 2013 版本 12.0.30110.00 Update 1 Microsoft .NET Framework 版本 4.5. ...

随机推荐

  1. 学习html5的WebSocket连接

    1.什么是WebSocket WebSocket 是一种自然的全双工.双向.单套接字连接.使用WebSocket,你的HTTP 请求变成打开WebSocket 连接(WebSocket 或者WebSo ...

  2. Rich IntelliSense for jQuery

    A while back we updated VS2008 IntelliSense to not fail when referencing jQuery.  However, getting I ...

  3. javascript 10进制和64进制的转换

    原文:javascript 10进制和64进制的转换 function string10to64(number) { var chars = '0123456789abcdefghigklmnopqr ...

  4. LINUX SCP 远程 文件 复制

    首先,以确保直接两个机器IP可以在每个ping通过 然后使用SCP命令从第一台主机向第二台主机复制文件 scp src chiwei@192.168.8.144:/home/chiwei/mydisk ...

  5. MY WAY程序(十八) 团队开发

    1.通信知识          要了解的强哥的移动通信先验知识布局.我和另外一个毕业生有自己的学习,但我真的没有认真看.了解了一下,其余大部分时间在搞nodejs.另外一个应届毕业生则是按着一本电子书 ...

  6. MVC 控制器激活

    MVC 控制器激活 ASP.NET MVC 控制器激活(三) 前言 在上个篇幅中说到从控制器工厂的GetControllerInstance()方法来执行控制器的注入,本篇要讲是在GetControl ...

  7. USBWebserver v8.6 PHP环境

    USBWebserver v8.6 Welcome on usbwebserver.com/net/eu USBWebserver v8.6 New in this version New langu ...

  8. Step one : 熟悉HTML

    //H1 1 <html> <head> <title>BeiJing</title> </head> <body> <h ...

  9. iOS基础 - 定时器

    1.可以完成的功能:每隔一段时间做一些固定的事情 2.创建定时器 1> 方法1 NSTimer *timer = [NSTimer timerWithTimeInterval:1.5 targe ...

  10. storm安装(3)storm本身的安装

    (6)下载storm包放入home文件夹中, 这里我用的版本是storm-0.8.2.zip 添加权限 chmod +x storm-0.8.2.zip 进行文件的解压 unzip storm-0.8 ...