CERC2017 H Hidden Hierarchy(树+模拟)
题意:
在一些给定的目录里按要求展开到制定大小并按字典序输出
思路:
因为有目录这个东西,所以想到模拟一个类似字典树的东西,不过这里每个儿子可能有n个节点,而且不能O(1)查询了
代码超长。。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<stack>
#include<queue>
#include<deque>
#include<set>
#include<vector>
#include<map>
#include<functional> #define fst first
#define sc second
#define pb push_back
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define lc root<<1
#define rc root<<1|1
#define lowbit(x) ((x)&(-x)) using namespace std; typedef double db;
typedef long double ldb;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PI;
typedef pair<ll,ll> PLL; const db eps = 1e-;
const int mod = 1e9+;
const int maxn = 2e6+;
const int maxm = 2e6+;
const int inf = 0x3f3f3f3f;
const db pi = acos(-1.0); //vector<string>str;//每个编号对应的字符串
string str[ + ];
bool cmp(int a, int b){
return str[a] < str[b];
}
vector<int>v[ + ];//v[i]为i的孩子们
int tol = ;
int vis[ + ];//结尾节点
int vl[ + ];//节点的值
int T;
void insert(vector<string>s, int val){
int root = ;
int sz = s.size();
for(int i = ; i < sz; i++){
//cout << i << " "<<s[i]<<endl;
int flg = ;
for(int j = ; j < (int)v[root].size(); j++){
if(str[v[root][j]] == s[i]){
root = v[root][j];
flg = ;
break;
}
}
if(!flg){
//cout << " bug "<<tol<<endl;
v[root].pb(tol);
str[tol] = s[i];
if(i==(sz-)){
vis[tol] = ;
vl[tol] = val;
}
root = tol++;
}
}
return;
}
int size[ + ];
int readydfs(int root){
int sz = v[root].size();
int res = ;
if(sz == ){
return vl[root];
}
int flg = ;
//cout << root << " "<<str[root]<<endl;
for(int i = ; i < sz; i++){
//cout << v[root][i] << " " << str[v[root][i]]<<endl;
if(vis[v[root][i]] != ){
flg = ;
}
res += readydfs(v[root][i]);
}
if(!flg) vis[root] = ; return vl[root] = res;
}
void dfs(int root, string s, int c){
int sz = v[root].size();
int flg = ;
if(vis[root]==)flg = ;
if(vl[root] < T){
if(vis[root] == ) flg = ;
else flg = ;
}
//cout << s <<endl;
/*if(flg == 0){
cout<<"- "<<s<<"/ "<<vl[root]<<endl;
}*/
if(flg == ){
cout <<" "<<s<<"/ "<<vl[root]<<endl;
return;
}
else if(flg == ){
cout << "+ "<<s<<"/ "<<vl[root]<<endl;
return;
}
sort(v[root].begin(), v[root].end(), cmp);
int vv = ;
for(int i = ; i < sz; i++){
if(vis[v[root][i]]!=){
if(vv &&vl[v[root][i]]>=T){
cout<<"- "<<s<<"/ "<<vl[root]<<endl;
vv = ;
break;
}
//if(flg == 3) dfs(v[root][i], s+"/"+str[v[root][i]], 0); }
}
if(!vv){
for(int i = ; i < sz; i++){
if(vis[v[root][i]]!=){ dfs(v[root][i], s+"/"+str[v[root][i]], ); }
}
}
else cout << "+ "<<s<<"/ "<<vl[root]<<endl;
return;
}
int main() {
tol = ;
str[] = "/";
mem(vis, );
mem(vl, );
ios::sync_with_stdio(false);
int n;
cin >> n;
string tmp;
for(int t = ; t <= n; t++){
int val;
cin >> tmp >> val;
int len = tmp.size();
vector<string>s;
int p = ;
for(int i = ; i < len; i++){
if(i && (tmp[i] == '/')){
s.pb(tmp.substr(p+, i-p-));
p = i;
}
if(i == len - ){
s.pb(tmp.substr(p+, i-p));
}
}
insert(s, val);
}
readydfs();
/*for(int i = 0; i < tol; i++){
cout << i <<" "<<vl[i]<<endl;
}*/
cin >> T;
string s = "";
dfs(, s, );
return ;
}
CERC2017 H Hidden Hierarchy(树+模拟)的更多相关文章
- Gym - 101620H_Hidden Hierarchy(树+模拟)
Hidden Hierarchy 题目链接 题目描述 You are working on the user interface for a simple text-based file explor ...
- hdu_5818_Joint Stacks(线段树模拟)
题目链接:hdu_5818_Joint Stacks 题意: 给你两个栈,多了个合并操作,然后让你模拟 题解: 很容易想到O(1)的单个栈操作,O(n)的合并操作,这样肯定超时,所以我们要将时间复杂度 ...
- 【CF280D】 k-Maximum Subsequence Sum ,线段树模拟费用流
昨天考试被教育了一波.为了学习一下\(T3\)的科技,我就找到了这个远古时期的\(cf\)题(虽然最后\(T3\)还是不会写吧\(QAQ\)) 顾名思义,这个题目其实可以建成一个费用流的模型.我们用流 ...
- 2019牛客暑期多校训练营(第八场)E:Explorer(LCT裸题 也可用线段树模拟并查集维护连通性)
题意:给定N,M,然后给出M组信息(u,v,l,r),表示u到v有[l,r]范围的通行证有效.问有多少种通行证可以使得1和N连通. 思路:和bzoj魔法森林有点像,LCT维护最小生成树. 开始和队友 ...
- hdu3436 splaytree树模拟队列+离散化缩点
数据较大,需要先把每个top不会操作到的段缩成一个点,记录其开始和结束的位置,和top能操作到的点一起建立一颗伸展树模拟 然后就是普通的队列模拟操作 /* 不会被top操作到的区间就缩点 通过spla ...
- poj2828 伸展树模拟
用伸展树模拟插队比线段树快乐3倍.. 但是pojT了.别的oj可以过,直接贴代码. 每次更新时,找到第pos个人,splay到根,然后作为新root的左子树即可 #include<iostrea ...
- C/C++深度优先搜索(递归树模拟)
//C++深度优先搜索(递归树模拟) #define _CRT_SECURE_NO_WARNINGS #include <iostream> #define MAX_N 1000 usin ...
- hdu 5475 模拟计算器乘除 (2015上海网赛H题 线段树)
给出有多少次操作 和MOD 初始值为1 操作1 y 表示乘上y操作2 y 表示除以第 y次操作乘的那个数 线段树的叶子结点i 表示 第i次操作乘的数 将1替换成y遇到操作2 就把第i个结点的值 替换成 ...
- 主席树/线段树模拟归并排序+二分答案(好题)——hdu多校第4场08
用主席树写起来跑的快一点,而且也很傻比,二分答案,即二分那个半径就行 主席树求的是区间<=k的个数 #include<bits/stdc++.h> using namespace s ...
随机推荐
- kubelet--help-v1.15.4
kubelet --help 官方文档 The kubelet is the primary "node agent" that runs on each node. It c ...
- 量化投资学习笔记07——python知识补漏
看<量化投资:以python为工具>这本书,第一部分是python的基础知识.这一部分略读了,只看我还不知道或不熟的. 定义复数 x = complex(2, 5) #2+5j 也可以直接 ...
- OAuth2.0概念以及实现思路简介
一.什么是OAuth? OAuth是一个授权规范,可以使A应用在受限的情况下访问B应用中用户的资源(前提是经过了该用户的授权,而A应用并不需要也无法知道用户在B应用中的账号和密码),资源通常以REST ...
- 原生javascript 元素依次掉落及上升
一.实现原理: ① 通过onoff开关,判断元素是往下走 还是往上走,并在每次清除定时器后,把onoff 设为 !onoff,以便下次点击做判断 ②move函数的运用 二.代码 <!DOCTYP ...
- linux下挂载硬盘出错的解决方法
我的电脑是 Uuntu16.04 + win10 双系统,今天在Ubuntu中打开D盘时报错 Error mounting /dev/sda5 原因是D盘的格式是ntfs,在linux中会出现不识别的 ...
- Java中的SPI扩展机制(有demo)
参考连接:https://www.jianshu.com/p/3a3edbcd8f24 一.什么是SPI SPI ,全称为 Service Provider Interface,是一种服务发现机制.它 ...
- Linux删除文件 清除缓存
相信很多测试 经常会经历开发叫你清除缓存这种事. 那我们要怎么清呢? 一.首先,确认你要清除的缓存在哪个目录下,然后切换到该目录下,比如 我现在知道我的的缓存目录是在newerp这个目录下,则如图 二 ...
- 洛谷 P1658 购物
题目链接 题目描述 你就要去购物了,现在你手上有N种不同面值的硬币,每种硬币有无限多个.为了方便购物,你希望带尽量少的硬币,但要能组合出1到X之间的任意值. 题目分析 题目要求组合出1到X之间的任意值 ...
- Spring(三)核心容器 - ApplicationContext 上下文启动准备
目录 前言 正文 第一步:prepareRefresh 第二步:obtainFreshBeanFactory 第三步:prepareBeanFactory 第四步:postProcessBeanFac ...
- 手把手写一个基于Spring Boot框架下的参数校验组件(JSR-303)
前言 之前参与的新开放平台研发的过程中,由于不同的接口需要对不同的入参进行校验,这就涉及到通用参数的校验封装,如果不进行封装,那么写出来的校验代码将会风格不统一.校验工具类不一致.维护风险高等其它因素 ...