POJ 3856 deltree

模拟题。

deltree
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 58 Accepted: 33

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.

Sample Input

>cd A
>dir
B
C
d 12
e 62
>cd B
>cd ..
>cd ..
>deltree A

>dir
G
s 2
>cd G
>dir
>cd \
>deltree G

>dir
A
B
x 3
>cd A
>dir
AA
AB
ax 10
ay 12
>cd AA
>dir
d 32
a 28
>cd ..
>cd AB
>dir
F
x 100
>cd F
>dir
G
>cd \
>deltree A
>cd D1\D2
>dir
D3
a 32
>cd D3
>dir
b 31
>cd \D1\D3
>dir
d 7
>deltree \D1

>exit

Sample Output

74
0
182
70

Source


#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#include <vector>
using namespace std;
struct T
{
    int file;
    vector<int> v;
};
T mem[1200000];
map<string,int> mp;
int lm;
char str[12000],cmd[12000],dic[12000],ts[12000];
int cdn,lc;
const int root=0;
int dfs(int x)
{
    int cnt,i;
    cnt=0;
    if (mem[x].file != -1)
        return mem[x].file;
    for (i=0; i<mem[x].v.size(); i++)
    {
        cnt+=dfs(mem[x].v[i]);
    }
    return cnt;
}
int main()
{
    int i,j,tn,ls,lts;
    bool flag;
    while (true)
    {
        mp.clear();
        mem[root].v.clear();
        mem[root].file=-1;
        cmd[0]='\\';
        cmd[1]='\0';
        lc=1;
        cdn=0;
        lm=0;
        mp[cmd]=lm;
        lm++;
        flag=false;
        while (true)
        {
            if (flag == false)
            {
                gets(str);
              //  printf("%s\n",str);
            }
           // printf("cmd=%s\n",cmd);
            flag=false;
            if (str[0] == '>' && str[1] == 'c' && str[2] == 'd')
            {
                sscanf(str,"%s%s",ts,dic);
  /*              printf("ts=%s dic=%s \n",ts,dic);     */
                if (strcmp(dic,"..") == 0)
                {
                    lc=strlen(cmd);
                    while (cmd[lc-2] != '\\')
                        lc--;
                    lc--;
                    cmd[lc]='\0';
                    cdn=mp[cmd];
                }
                else
                {
                    if (dic[0] == '\\')
                    {
                        cmd[0]='\\';
                        cmd[1]='\0';
                        lc=1;
                        cdn=0;
                        strcpy(dic,dic+1);
                    }
                    if (dic[0] == '\0')
                        continue;
                    for (i=0,j=0; ; i++)
                    {
                        if (dic[i] == '\\' || dic[i] == '\0')
                        {
                            ts[j]='\\';
                            j++;
                            ts[j]='\0';
                         //   printf("ts=%s\n",ts);
                            strcpy(ts,strcat(cmd,ts));
                          //  printf("ts=%s\n",ts);
                            if (mp.find(ts) == mp.end())
                            {
                                mp[ts]=lm;
                                mem[lm].file=-1;
                                mem[lm].v.clear();
                                mem[cdn].v.push_back(lm);
                                strcpy(cmd,ts);
                                cdn=lm;
                                lm++;
                            }
                            else
                            {
                                strcpy(cmd,ts);
                                cdn=mp[cmd];
                            }
                            if (dic[i] == '\\')
                                j=0;
                            else
                                break;
                        }
                        else
                        {
                            ts[j]=dic[i];
                            j++;
                        }
                    }
                }
            }
            else if (str[0] == '>' && str[1] == 'd' && str[2] == 'i')
            {
                flag=true;
                while (true)
                {
                    gets(str);
                    ls=strlen(str);
                    if (str[0] == '>')
                        break;
                    for (i=0; str[i]; i++)
                    {
                        if (str[i] == ' ')
                            break;
                    }
                    if (str[i] == '\0')
                    {
                        str[ls]='\\';
                        ls++;
                        str[ls]='\0';
                        strcpy(dic,cmd);
                        strcat(dic,ts);
                        if (mp.find(dic) == mp.end())
                        {
                            mp[dic]=lm;
                            mem[lm].file=-1;
                            mem[lm].v.clear();
                            mem[cdn].v.push_back(lm);
                            lm++;
                        }
                    }
                    else
                    {
                        sscanf(str,"%s%d",ts,&tn);
                        lts=strlen(ts);
                        ts[lts]='\\';
                        lts++;
                        ts[lts]='\0';
                        strcpy(dic,cmd);
                        strcat(dic,ts);
                        if (mp.find(dic) == mp.end())
                        {
                            mp[dic]=lm;
                            mem[lm].file=tn;
                            mem[lm].v.clear();
                            mem[cdn].v.push_back(lm);
                            lm++;
                        }
                    }
                }
            }
            else if (str[0] == '>' && str[1] == 'd' && str[2] == 'e')
            {
                sscanf(str,"%s%s",ts,dic);
                if (dic[0] == '\\')
                {
                    strcpy(cmd,dic);
                    lc=strlen(cmd);
                    cmd[lc]='\\';
                    lc++;
                    cmd[lc]='\0';
                }
                else
                {
                    strcpy(cmd,strcat(cmd,dic));
                    lc=strlen(cmd);
                    cmd[lc]='\\';
                    lc++;
                    cmd[lc]='\0';
                }
                if (mp.find(cmd) == mp.end())
                    printf("0\n");
                else
                {
                    cdn=mp[cmd];
                    printf("%d\n",dfs(cdn));
                }
                break;
            }
            else if (str[0] == '>' && str[1] == 'e')
            {
                return 0;
            }
        }
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值