ZOJ 3965 Binary Tree Restoring
思路:
递归
比较a序列和b序列中表示同一个子树的一段区间,不断递归
代码:
#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define pb push_back
#define mem(a,b) memset(a,b,sizeof(a)) const int N=1e5+;
int a[N],b[N],par[N],posa[N],posb[N],sum[N],n;
void dfs(int l,int r,int ll,int rr,int pa){
if(l>r||ll>rr||r>n||rr>n)return ;
if(a[l]==b[ll]){
par[a[l]]=pa;
sum[pa]++;
dfs(l+,r,ll+,rr,a[l]);
}
else{
par[a[l]]=par[b[ll]]=pa;
sum[pa]+=;
int len1=posa[b[ll]]-l;
int len2=posb[a[l]]-ll;
dfs(l+,l+len1-,posb[a[l]]+,posb[a[l]]+len1-,a[l]);
dfs(posa[b[ll]]+,posa[b[ll]]+len2-,ll+,ll+len2-,b[ll]);
while(sum[pa]==)pa=par[pa];
dfs(l+len1+len2,r,ll+len1+len2,rr,pa);
}
}
int main(){
int T;
scanf("%d",&T);
while(T--){
scanf("%d",&n);
for(int i=;i<=n;i++)scanf("%d",&a[i]),posa[a[i]]=i;
for(int i=;i<=n;i++)scanf("%d",&b[i]),posb[b[i]]=i;
mem(sum,);
dfs(,n,,n,);
for(int i=;i<=n;i++)printf("%d%c",par[i]," \n"[i==n]);
}
return ;
}
ZOJ 3965 Binary Tree Restoring的更多相关文章
- zoj 3965 Binary Tree Restoring(搜索)
Binary Tree Restoring Time Limit: 1 Second Memory Limit: 65536 KB Special Judge Given two ...
- 2017浙江省赛 H - Binary Tree Restoring ZOJ - 3965
地址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3965 题目: iven two depth-first-search ...
- ZOJ3965 Binary Tree Restoring
ZOJ3965 给定一颗二叉树的两种DFS序列 输出一种可能的二叉树的结构. 考察树的递归性质,不要想的太复杂. 当前节点在两个串中后面的节点假如不同则能确认两个子树,如果相同则把下个点作当前点的一个 ...
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- Leetcode, construct binary tree from inorder and post order traversal
Sept. 13, 2015 Spent more than a few hours to work on the leetcode problem, and my favorite blogs ab ...
- [LeetCode] Find Leaves of Binary Tree 找二叉树的叶节点
Given a binary tree, find all leaves and then remove those leaves. Then repeat the previous steps un ...
- [LeetCode] Verify Preorder Serialization of a Binary Tree 验证二叉树的先序序列化
One way to serialize a binary tree is to use pre-oder traversal. When we encounter a non-null node, ...
- [LeetCode] Binary Tree Vertical Order Traversal 二叉树的竖直遍历
Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bott ...
随机推荐
- 叶亚明:合格CTO的六要素(转)
叶亚明,携程旅行网CTO & 高级技术副总裁,负责携程的移动.Online.呼叫中心等的技术架构.开发及运营.在加入携程之前,叶亚明是ebay.com技术平台总监,领导ebay.com几代网站 ...
- 翻译 Improved Word Representation Learning with Sememes
翻译 Improved Word Representation Learning with Sememes 题目 Improved Word Representation Learning with ...
- win7安装mysql-8.0.13-winx64
这里展示一下,由于需要安装一个版本测试一下数据,其实就是超简单的啦. 下包 注:https://dev.mysql.com/downloads/mysql/ 解压与配置 [mysqld] basedi ...
- redhat6.4 gcc g++ rpm方式安装步骤
centos源 包包自行下载 [root@localhost gcc]# rpm -ivh ppl-0.10.2-11.el6.x86_64.rpm warning: ppl-0.10.2-11.el ...
- Codeforces Round #427 (Div. 2) Problem C Star sky (Codeforces 835C) - 前缀和
The Cartesian coordinate system is set in the sky. There you can see n stars, the i-th has coordinat ...
- java利用poi生成excel文件后下载本地
1.该功能需要poi的jar包,链接: http://pan.baidu.com/s/1migAtNq 密码: 38fx. 2.首先新建一个实体类,用以存放单个数据 public class Test ...
- Bootstrap3基础 form-horizontal 表单元素横向布局 简单示例
内容 参数 OS Windows 10 x64 browser Firefox 65.0.2 framework Bootstrap 3.3.7 editor ...
- 【第四十章】Spring Boot 自定义拦截器
1.首先编写拦截器代码 package com.sarnath.interceptor; import javax.servlet.http.HttpServletRequest; import ja ...
- 什么是SpringCloud
SpringCloud是在SpringBoot的基础上构建的,用于简化分布式系统构建的工具集. 该工具集为微服务架构中所涉及的配置管理,服务发现,智能路由,断路器,微代理和控制总线等操作 提供了一种简 ...
- 日系插画学习笔记(一):SAI软件基础
检测驱动是否安装正确:1.画笔没有压感,线条没有粗细变化2.画笔线条有锯齿 一.文件:新建文件:预设尺寸:一般选择A3(8k),A4(16k),A5(32k)作业要求:A4A5 - 300dpi,像素 ...