Description

You have just run out of disk space and decided to delete some of your directories. Rationally, you will first have an exploration of what you have in your file system. And more rationally, you will do this exploration through a command line interface. The interface used in this problem is called “MSDOS--”, since it is something like MSDOS with fewer features. The commands of MSDOS-- are as follows: 
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

Input contains multiple independent scenarios. There is an empty line after each scenario. The input ends with an “exit” command. There is a “>” sign before each command in the input (with no spaces in between). The length of each file name does not exceed 50. You may assume that the input is correct.

Output

Write the result of the ith scenario as a single integer on the ith line of 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(模拟)的更多相关文章

  1. poj 3077Rounders(模拟)

    转载请注明出处:viewmode=contents">http://blog.csdn.net/u012860063? viewmode=contents 题目链接:http://po ...

  2. POJ 1068 Parencodings 模拟 难度:0

    http://poj.org/problem?id=1068 #include<cstdio> #include <cstring> using namespace std; ...

  3. POJ 1036 Rails 模拟堆栈

    水题,主要是思路清晰,判断明确. 记x为A站最前方的车,y表示下一列要进入B站的车厢,初识时,x=1;y=a1;C=[]; 在调度过程中: if(y==0)那么调度成功,退出模拟过程:否则 if(x= ...

  4. POJ 1001 Exponentiation 模拟小数幂

    模拟小数幂 小数点位 pos 非零末位 e 长度 len 只有三种情况 pos > len pos < e e < pos < len #include <iostrea ...

  5. POJ 1008 简单模拟题

    e.... 虽然这是一道灰常简单的模拟题.但是米做的时候没有读懂第二个日历的计时方法.然后捏.敲完之后华丽的WA了进一个点.坑点就在一年的最后一天你是该输出本年的.e ...但是我好想并没有..看di ...

  6. Crashing Robots POJ 2632 简单模拟

    Description In a modernized warehouse, robots are used to fetch the goods. Careful planning is neede ...

  7. poj 1806 分块模拟

    Manhattan 2025 Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 1318   Accepted: 703 Des ...

  8. poj 1472(递归模拟)

    题意:就是让你求出时间复杂度. 分析:由于指数最多为10次方,所以可以想到用一个数组保存各个指数的系数,具体看代码实现吧! 代码实现: #include<cstdio> #include& ...

  9. poj 1068 Parencodings 模拟

    进入每个' )  '多少前' (  ', 我们力求在每' ) '多少前' )  ', 我的方法是最原始的图还原出来,去寻找')'. 用. . #include<stdio.h> #incl ...

随机推荐

  1. Python基础—10-常用模块:time,calendar,datetime

    #常用模块 time sleep:休眠指定的秒数(可以是小数) time:获取时间戳(从1970-01-01 00:00:00到此刻的秒数) localtime:将一个时间戳转换为一个对象,对象中包含 ...

  2. 选择排序_C语言_数组

    选择排序_C语言_数组 #include <stdio.h> void select_sort(int *); int main(int argc, const char * argv[] ...

  3. Webpack4 学习笔记七 跨域服务代理

    webpack 小插件使用 webpack 监听文件变化配置 webpack 处理跨域问题 Webpack 小插件使用 clean-webpack-plugin: 用于在生成之前删除生成文件夹的Web ...

  4. hdu_2067_小兔的棋盘

    小兔的叔叔从外面旅游回来给她带来了一个礼物,小兔高兴地跑回自己的房间,拆开一看是一个棋盘,小兔有所失望.不过没过几天发现了棋盘的好玩之处.从起点(0,0)走到终点(n,n)的最短路径数是C(2n,n) ...

  5. Linux安装redis PHP安装Redis扩展 and基本命令

    一.安装redis 用超级管理员身份运行: $ mkdir /usr/local/redis #redis安装目录 $ cd /usr/local/src #安装包下载目录 $ wget http:/ ...

  6. JavaScript文本框焦点事件

    效果图如下: <!-- 当文本框获得焦点时候,如果文本框内容是 请输入搜索关键字 清空文本框,输入内容变黑色 --> <!-- 当文本框失去焦点时候,如果文本框无内容,则添加灰色的 ...

  7. MySQL数据库操作(DDL)

    一.创建数据库 语法:create database 数据库名称 [库选项]; 库选项:(可选)数据库的属性,一般有字符集与校对集,保存在数据库所属文件夹下的opt文件 charset:字符集,表示该 ...

  8. queue消息队列

    class queue.Queue(maxsize=0) #先入先出 class queue.LifoQueue(maxsize=0) #last in fisrt out  class queue. ...

  9. liunx下搭建python开发环境

    =============================================================================注意: 在linux下安装新的版本的pytho ...

  10. ctf题目writeup(9)

    继续刷题,找到一个 什么 蓝鲸安全的ctf平台 地址:http://whalectf.xin/challenges (话说这些ctf平台长得好像) 1. 放到converter试一下: 在用十六进制转 ...