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. swiper插件使用技巧

    1.加载插件: <!DOCTYPE html> <html> <head> ... <link rel="stylesheet" href ...

  2. 前端DOM知识点

    DOM即文档对象模型(Document Object Model,DOM)是一种用于HTML和XML文档的编程接口.它给文档提供了一种结构化的表示方法,可以改变文档的内容和呈现方式.DOM把网页和脚本 ...

  3. vue组件原生事件以及路由

    1.组件 组件就是可以扩展HTML元素,封装可重用的HTML代码,可以将组件看作自定义的HTML元素 1.1组件注册 全局注册: 组件注册时,需要给他一个名字,如下: Vue.component('m ...

  4. Java实现非递归归并排序

    public class nonRecursiveMergeSort { public static void main(String[] args) { int[] list = {8,4,3,6, ...

  5. UDP端口启动后一段时间无法接收到数据

    接口需求:开发一个UDP协议的接口作为服务端接收来自客户端的认证数据,数据量每分钟7w+条; 数据格式:标准的redius协议,redius协议的相关知识在网上查资料,提供线索:http://blog ...

  6. 微信小程序学习笔记(一)

    1.目录及文件构成 1.1 根目录下 ** app.js 是小程序的脚本代码,用来监听并处理小程序的生命周期函数.声明全局变量. ** app.json 是对整个小程序的全局配置,配置小程序是由哪些页 ...

  7. linux的date常用命令

    1.显示现在时间 date 2.显示今天日期 date +"%F" date +"%Y-%m-%d" 3.现在时间转化为时间戳 date +%s 4.指定某日期 ...

  8. 8-C++远征之继承篇-学习笔记

    C++远征之继承篇 开篇介绍 整个C++远征计划: 起航->离港->封装->继承 为什么要用继承? 为什么要有继承? 如何来定义基类 <----> 派生类? 基类到派生类 ...

  9. [BZOJ1040][ZJOI2008]骑士(树形DP)

    对于一个联通块内,有且只有一个环,即n个点n条边 那么找到那个环,然后任意断一条边,这个联通块就变成一棵树了,然后做树形DP就行了 对于断的边要记录下来DP时特判 Code #include < ...

  10. 安装和配置Sentry(收录)

    安装和配置Sentry 本文主要记录安装和配置Sentry的过程,关于Sentry的介绍,请参考 Apache Sentry架构介绍 . 1. 环境说明 系统环境: 操作系统:CentOs 6.6 H ...