HDU5444 Elven Postman
2024-09-05 13:50:04
按要求递归建树输出~
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn=;
struct node {
int data;
node * left;
node * right;
};
int pre[maxn],in[maxn],T,N,q,x;
void init () {
fill (pre,pre+maxn,);
fill (in,in+maxn,);
}
node * create (int preL,int preR,int inL,int inR) {
if (preL>preR) return NULL;
node * root=new node;
root->data=pre[preL];
int k;
for (k=inL;k<=inR;k++) {
if (in[k]==pre[preL]) break;
}
int numLeft=k-inL;
root->left=create(preL+,preL+numLeft,inL,k-);
root->right=create(preL+numLeft+,preR,k+,inR);
return root;
}
void dfs (node * root,int v) {
if (root->data==v) {
printf ("\n");
return;
}
if (v<root->data) printf ("E"),dfs (root->left,v);
else printf ("W"),dfs (root->right,v);
}
int main () {
scanf ("%d",&T);
while (T--) {
scanf ("%d",&N);
for (int i=;i<=N;i++) in[i]=i;
for (int i=;i<=N;i++) scanf ("%d",&pre[i]);
node * root=create(,N,,N);
scanf ("%d",&q);
for (int i=;i<q;i++) scanf ("%d",&x),dfs (root,x);
}
return ;
}
最新文章
- 初识CoreData与详解
- ReactiveCocoa(RAC)
- 第一节Unity3D简介
- 我所了解的cgi(转)
- Coder-Strike 2014 - Finals (online edition, Div. 2) C题
- 高性能IO设计的Reactor和Proactor模式(转)
- No enclosing instance of type Outer is accessible. Must qualify the allocation with an enclosing instance of type Outer (e.g. x.new A() where x is an instance of Outer)
- .Net程序员学用Oracle系列(8):触发器、任务、序列、连接
- Linux中kettle自动化部署脚本
- UVa 11503 - Virtual Friends
- hdu 5391 (数论)
- mysql学习笔记--数据库事务
- 使用SSH命令行传输文件到远程服务器
- Cocos Creator cc.Event点击触摸事件详解
- 异常:unity3d ArgumentException: The Assembly System.Configuration is referenced by System.Data.
- Emmagee——开源Android性能测试工具
- MFC中UpdateData()函数的使用
- Python中type的用法
- C# unity 的 IInterceptionBehavior实现aop拦截器
- 〖Android〗(how-to) fix k860/k860i buletooth.