POJ 3856 deltree(模拟)
Description
1. cd <directory> Assuming <directory> to be the name of a relative descendant of current directory, this command changes the current directory to <directory>. For example, when the current directory is “\A\B\” and one of its descendants is “C\D”, the execution of “cd C\D” will change the current directory to “\A\B\C\D\”.
2. cd \ This command changes the current directory to “\” (the root of the file system). For example, when the current directory is “\A\B\”, the execution of “cd \” will change the current directory to “\”.
3. cd .. Assuming the current directory to be anything except “\”, this command changes the current directory to its parent directory. For example, when the current directory is “\A\B\”, the execution of “cd ..” will change the current directory to “\A\”.
4. cd \<directory> This command is equivalent to the execution of the following two commands: cd \ cd <directory>
5. dir This command lists the name of files and directories directly in the current directory, each on a separate line. These file/directory names are made up of (lowercase and uppercase) letters, digits, and dots (“.”). Directory names precede the file names in the list, and each one, comes alone in a single line. On the contrary, each file name is accompanied by its size separated by a space. A sample output of “dir” is as follows: HW1 HW1.old Syllab.pdf 10000 notes.txt 3241
6. deltree <directory> Assuming <directory> to be the name of a relative descendant of current directory, this command tries to delete <directory> and all its descendant files and subdirectories (and thus, freeing that much of space). For example, when the current directory is “\A\B\” and one of its descendants is “C\D”, the execution of “deltree C\D” will try to delete directory “\A\B\C\D\” and all of its descendant files and directories.
7. deltree \<directory> This command is equivalent to the execution of the following two commands: cd \ deltree <directory>
8. exit This command terminates the command line interface.
A “scenario” is an exploration (a consistent series of “cd” and “dir” commands and their results, starting from root) followed by exactly one “deltree” command. Given a scenario, you are to find the maximum space guaranteed to be freed by executing its “deltree” command.
Input
Output
题目大意:模拟一个CMD的运行,假定所有给定的语句都是正确的。
思路:丧心病狂模拟题系列。注意细节,比如我用一个目录dir两次,不要同一个文件算两次,再如有A\B,我删掉了B,然后再删A的时候,不要再把B的容量给算上了。我觉得这题样例还算有良心,我过了样例就AC了o(╯□╰)o
PS:我的代码虽然暴力是暴力了点,不过丧心病狂模拟题的重点,不是要快,而是要好写,好调,准确……
代码(0MS):
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std; const int MAXN = ; struct Node {
char name[];
int next, pre, siz;
bool isFile, del;
}; Node a[MAXN];
int head[MAXN], ecnt, root, now;
char s[], tmp[]; void clear() {
root = now = ;
head[root] = ;
ecnt = ;
} int new_sub(int cur, char *name, int size, bool isFile) {
a[ecnt].pre = cur;
strcpy(a[ecnt].name, name);
a[ecnt].siz = size;
a[ecnt].isFile = isFile;
a[ecnt].next = head[cur];
a[ecnt].del = ;
head[ecnt] = ;
return head[cur] = ecnt++;
} int to_sub(int cur, char *name, int siz = , int isFile = ) {
for(int p = head[cur]; p; p = a[p].next)
if(strcmp(a[p].name, name) == ) return p;
return new_sub(cur, name, siz, isFile);
} int get_pre(int cur) {
return a[cur].pre;
} int get_root() {
return root;
} void _strcpy(char *&src, char *tar) {
int len = ;
while(*src != '\\' && *src != ) tar[len++] = *src, ++src;
tar[len] = ;
if(*src == '\\') ++src;
} void cd(char *s) {
if(*s == '\\') now = get_root(), ++s;
while(*s != ) {
_strcpy(s, tmp);
now = to_sub(now, tmp);
}
} int str_to_num(char *s) {
int ret = ;
for(int i = ; s[i]; ++i)
ret = ret * + s[i] - '';
return ret;
} void dir() {
while(gets(s) && *s != '>') {
int i = ;
for(i = ; s[i]; ++i)
if(s[i] == ' ') break;
if(s[i] != ' ') {
to_sub(now, s);
}
else {
s[i] = ;
to_sub(now, s, str_to_num(s + i + ), );
}
}
} int dfs_del(int cur) {
if(a[cur].del) return ;
a[cur].del = true;
int ret = ;
for(int p = head[cur]; p; p = a[p].next) {
if(a[p].isFile) ret += a[p].siz;
else ret += dfs_del(p);
}
return ret;
} void deltree(char *s) {
if(*s == '\\') now = root, ++s;
int cur = now;
while(*s != ) {
_strcpy(s, tmp);
cur = to_sub(cur, tmp);
}
printf("%d\n", dfs_del(cur));
} int main() {
clear();
gets(s);
while(strcmp(s, ">exit") != ) {
if(s[] == ) {//next exploration
clear();
gets(s);
}
if(strcmp(s, ">cd ..") == ) {
now = get_pre(now);
gets(s);
continue;
}
if(strncmp(s, ">cd", ) == ) {
char *name = s + ;
while(*name == ' ') ++name;
cd(name);
gets(s);
continue;
}
if(strcmp(s, ">dir") == ) {
dir();
continue;
}
if(strncmp(s, ">deltree", ) == ) {
char *name = s + ;
while(*name == ' ') ++name;
deltree(name);
gets(s);
continue;
}
}
}
POJ 3856 deltree(模拟)的更多相关文章
- poj 3077Rounders(模拟)
转载请注明出处:viewmode=contents">http://blog.csdn.net/u012860063? viewmode=contents 题目链接:http://po ...
- POJ 1068 Parencodings 模拟 难度:0
http://poj.org/problem?id=1068 #include<cstdio> #include <cstring> using namespace std; ...
- POJ 1036 Rails 模拟堆栈
水题,主要是思路清晰,判断明确. 记x为A站最前方的车,y表示下一列要进入B站的车厢,初识时,x=1;y=a1;C=[]; 在调度过程中: if(y==0)那么调度成功,退出模拟过程:否则 if(x= ...
- POJ 1001 Exponentiation 模拟小数幂
模拟小数幂 小数点位 pos 非零末位 e 长度 len 只有三种情况 pos > len pos < e e < pos < len #include <iostrea ...
- POJ 1008 简单模拟题
e.... 虽然这是一道灰常简单的模拟题.但是米做的时候没有读懂第二个日历的计时方法.然后捏.敲完之后华丽的WA了进一个点.坑点就在一年的最后一天你是该输出本年的.e ...但是我好想并没有..看di ...
- Crashing Robots POJ 2632 简单模拟
Description In a modernized warehouse, robots are used to fetch the goods. Careful planning is neede ...
- poj 1806 分块模拟
Manhattan 2025 Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 1318 Accepted: 703 Des ...
- poj 1472(递归模拟)
题意:就是让你求出时间复杂度. 分析:由于指数最多为10次方,所以可以想到用一个数组保存各个指数的系数,具体看代码实现吧! 代码实现: #include<cstdio> #include& ...
- poj 1068 Parencodings 模拟
进入每个' ) '多少前' ( ', 我们力求在每' ) '多少前' ) ', 我的方法是最原始的图还原出来,去寻找')'. 用. . #include<stdio.h> #incl ...
随机推荐
- Django-rest-framework(二)serializers 使用
简介 初次见到serializers文件,想必大家都会感到陌生,所以,我们不妨换个词来形容他的作用,那就是django 中的Form,这样是不是感觉熟悉了一点. 实际上,serializers 的作用 ...
- 菜鸟笔记 -- Chapter 6.2.3 成员变量
6.2.3 成员变量 在Java中对象的属性也称为成员变量[也叫字段].成员变量的类型可以设置为Java中合法的数据类型,其实成员变量就是普通的变量,可以为它设置初始值,也可以不设置初始值,如果不设 ...
- Question 20171115 String&&StringBuffer&&StringBuilder的区别与联系?
Question 20171114 String&&StringBuffer&&StringBuilder的区别和联系 创建成功的String对象,其长度是固定的,内容 ...
- 辨析:Object与Instance都是对象,概念上没有区别。
Object与Instance有重要的区别:Object是客观世界中存在的实体:Instance是将Object虚拟到计算机世界的实例,它的生存方式是可运行的代码,它的生存环境是计算机中的内存资源,生 ...
- Lucene的原理和应用
随着互联网的迅速普及与发展,网络舆论对社会生活的影响力越来越大, 网络口碑研究也逐渐形成一个新兴行业.有效的网络口碑研究,需要全方位地倾听网民的声音. 信息检索技术的应用,有效地提高了网络口碑研究的工 ...
- 原生js的常见封装
)); } ;;;;]){ ]; ] = ;;;,) ,) ,) ,) ,) , ...
- Generating Complex Procedural Terrains Using GPU
前言:感慨于居然不用tesselation也可以产生这么复杂的地形,当然致命的那个关于不能有洞的缺陷还是没有办法,但是这个赶脚生成的已经足够好了,再加上其它模型估 计效果还是比较震撼的.总之好文共分享 ...
- ERROR: bootstrap checks failed
错误描述:Linux默认配置的参数过小,需要自己设置 max file descriptors [4096] for elasticsearch process is too low, increas ...
- u-boot、kernel、root系统烧写和挂载命令命令
一.uboot 环境变量: 1. 打印环境变量:# print 2. 设置启动参数# set bootargs noinitrd init=/linuxrc console=ttySAC0,11520 ...
- python爬虫之有道在线翻译
今天初学了python这门课 老师简单的讲解了一下 python的安装环境,配置环境变量,当前主流Python使用的是3.x版本, 下午简单的讲解了python的起源,发展以及在各个方面的应用 然后晚 ...