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 ...
随机推荐
- Mac 使用问题
Mac 使用 Mac改变系统截图存储位置 鼠标在屏幕中间上下滚动时,有时反应不灵敏: 看看偏好设置-> 鼠标 -> 跟踪速度或者手势部分是否有哪里设置不当: 考虑重新开启鼠标鼠标底部有一个 ...
- 协议类接口 - I2C
一.12c总线概述 I2C( Inter-Integrated Circuit,又称IIC)总线是一种串行总线,用 于连接微控制器及其外围设备,硬件图非常简单:一条串行数据线(SDA),一条串行时钟线 ...
- iOS10 开发权限适配设置 崩溃(上传打包后构建版本一直不显示)
ios10 系统必须强制配置系统权限 如果不配置,调试的时候导致崩溃,还会引发包无效的问题,导致上传打包后构建版本一直不显示 解决方案1.在项目中找到info.plist文件,右键点击以 Source ...
- 简明 ES6 模块
简明 ES6 模块 1.什么是模块 模块就是一段代码,这段代码可以反复使用,经常单独写成一个文件,一旦加载会立即执行. 2.导出 导出有 2 种方式:命名导出和默认导出,分别用关键字export和ex ...
- POJ 2398--Toy Storage(叉积判断,二分找点,点排序)
Toy Storage Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6534 Accepted: 3905 Descr ...
- 【CodeForces 803 C】Maximal GCD(GCD+思维)
You are given positive integer number n. You should create such strictly increasingsequence of k pos ...
- Jensen 不等式
若f(x)为区间I上的下凸(上凸)函数,则对于任意xi∈I和满足∑λi=1的λi>0(i=1,2,...,n),成立: \[f(\sum ^{n} _{i=1} \lambda _{i}x_{i ...
- 搭建mysql主从复制和删库数据恢复策略
搭建主从复制 主机: [mysqld] 下增加 vim /etc/my.cnf ## 设置 server_id,一般设置为 IP server_id=8 # # 复制过滤:需要备份的数据库,输出 bi ...
- python list统计
from random import randint data = [randint(0, 20) for _ in xrange(30)] print data # [20, 4, 4, 20, 1 ...
- 使用zxing二维码识别
1.多二维码识别 (同一张图片中多二维码识别) 直接上代码舒服: pom文件: <!-- QR Code --> <dependency> <groupId>com ...