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. JSON 分析数据格式

    2014年4月26日本10:55:15 从三月初开始,并JSON战斗.学校不教怎样做,己..大规模的搜索啊.发表这篇文章,喜欢我和不知道如何开始的新手! 和 XML 如.JSON 也是基于纯文本数据格 ...

  2. Grunt使用入门

    Grunt使用入门 (by vczero) 一.前言 项目中一直在使用Grunt,只是对Grunt的基本使用,却未系统的总结过.为什么要构建工具?一句话:自动化.对于需要反复重复的任务,例如压缩(mi ...

  3. C#放缩、截取、合并图片并生成高质量新图的类

    原文:C#放缩.截取.合并图片并生成高质量新图的类 using System;using System.Drawing;using System.Drawing.Imaging;using Syste ...

  4. DDD领域驱动设计

    DDD领域驱动设计实践篇之如何提取模型 需求说明: 省级用户可以登记国家指标 省级用户和市级用户可以登记指标分解 登记国家指标时,需要录入以下数据:指标批次.文号.面积,这里省略其他数据,下同 登记指 ...

  5. ios中mvc的FormsAuthentication.SetAuthCookie(cookieUserName, false)失败

    如果楼主使用.net开发,要注意FormsAuthentication.SetAuthCookie 方法的使用会导致ios出现该问题.因为这个方法在ios设备上是把票据加入到url中,导致url和你的 ...

  6. SZU:L89 Frog Encoding

    Judge Info Memory Limit: 65536KB Case Time Limit: 3000MS Time Limit: 3000MS Judger: Normal Descripti ...

  7. iOS基础 - 核心动画

    一.核心动画 l 核心动画基本概念 l 基本动画 l 关键帧动画 l 动画组 l 转场动画 l Core Animation是一组非常强大的动画处理API,使用它能做出非常炫丽的动画效果,而且往往是事 ...

  8. treeview和listview的用法

    今天无聊中弄了个小东西,熟悉一下对listview和treeview的用法.代码如下:  public partial class Form1 : Form     {         private ...

  9. D13

    =-=由于本人有极度强迫症啊.. 然后这个博客又不能改顺序.. 前几天由于台风是在宾馆写题..简直各种没有效率..所以今天就先草草写下题解,之后再完善吧 T1:字符串处理 c++的话,解决读空格继续读 ...

  10. Linux脚本学习随记

    把文件件的归属转移到其他用户上chown [-R] 账号名称:用户组名称 文件或目录 在进行hadoop分布式部署的时候,需要生成密钥对具体的操作如下先在master的hadoop目录下创建.sshm ...