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 ...
随机推荐
- JavaScript的原型(prototype、__proto__、constructor)
构造函数:function Foo() {}; 实例对象: let f1 = new Foo; let o1 = new Foo; 一般函数都有prototype属性,除了window.Math和Fu ...
- ORA-12154/ORA-12560 可以尝试的解决办法
WIN10 本机安装ORACLE数据库和ORACLE客户端后,使用PL/SQL 登陆提示错误ORA-12154 和ORACLE-12560, 在检查了本机的注册表.环境PATH路径.tnsnames ...
- Python基础—05-总结:双重循环,数据类型
总结 双重循环 冒泡排序 lt = [1, 5, 7, 3, 2, 4] # 计算元素的个数 n = len(lt) # 外层循环控制圈数 for i in range(n-1): for j in ...
- http返回值含义
1xx:信息响应类,表示接收到请求并且继续处理2xx:处理成功响应类,表示动作被成功接收.理解和接受 3xx:重定向响应类,为了完成指定的动作,必须接受进一步处理4xx:客户端错误,客户请求包含语法错 ...
- 【杂题总汇】HDU-5215 Cycle
◆HDU-5215◆ Cycle 国庆节集训的第三天……讲图论,心情愉快……刷了一堆水题,不过也刷了一些有意思的题 +传送门+ HDU ▶ 题目 给出一个无向图(无自环,无重边),求该无向图中是否存在 ...
- Redis安装与简单配置
一.Redis介绍 1.redis是什么? remote dIctionary server(Redis) 是一个由Salvatore Sanfilippo写的key-value存储系统.Redis提 ...
- JavaScript--Dom操作元素的样式
在前端开发中,有时候需要动态修改的网页元素的样式,这里将使用JS动态修改元素样式的方法做个小结: 网页结构: 按钮: 标签:input 类型:button id:btn ...
- flex布局入门总结——语法篇
前几天看了阮一峰的Flex布局教程,讲的很不错,总结一下,有兴趣的可以去看原文http://www.ruanyifeng.com/blog/2015/07/flex-grammar.html 一 F ...
- php 移动或重命名文件(图片)到另一目录下的方法有多种,这里只列出三种:
php 移动或重命名文件(图片)到另一目录下的方法有多种,这里只列出三种: 方法一:使用copy函数 格式:copy(source,destination) 将文件从 source ...
- C语言关于指针的注意事项
一.指针的四个关键概念1.指针的类型2.指针指向的类型3.指针的值,也就是指针指向的地址4.指针自己所占用的内存空间注意:指针变量所存的内容就是内存的地址编号! 例如:int **pp = NULL; ...