【CCF 路径解析】

= =简直… 上来一发80分,然后一直想哪里错了,然后发现是考虑了名字中带.的路径,没考虑路径为..xxxx的情况…补上之后90分……= =!!然后想可能是之前用string,不支持空行的缘故,所以又改成了char,然后就可以了

注意当前路径也有可能是需要解析的,两端代码几乎一样……迷之微笑,不想优化

#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
#define maxn 1010
string que[maxn],tmp[maxn];
char s[maxn];
int main()
{
    int n;
    cin>>n;
    getchar();
    gets(s);
    int pre=0;
    int top=0;
    for(int i=0;s[i]!='\0';i++)
    {
        if(s[i]=='/'||s[i]==' ') continue;
        if(s[i]=='.')
        {
            if(s[i+1]=='.' && s[i+2]=='/')
            {
                top--,i++;
                if(top<0) top=0;
                continue;
            }
            else if(s[i+1]=='/') continue;
        }
        
            string tt ="";
            int j;
            for(j=i;s[j]&&s[j]!='/';j++)
                tt+=s[j];
            que[top++]=tt;
            i=j;
    }
    
    while(n--)
    {
        
        gets(s);
        
        int p;
        for(int i=0;i<maxn;i++) tmp[i]="";
        if(s[0]=='/') p=0;
        else
        {
            for(int i=0;i<top;i++) tmp[i]=que[i];
            p=top;
        }
        if(strlen(s))
        {
            for(int i=0;i<strlen(s);i++)
            {
                if(s[i]=='/'||s[i]==' ') continue;
                if(s[i]=='.')
                {
                    if(s[i+1]=='.' && s[i+2]=='/')
                    {
                        p--,i++;
                        if(p<0) p=0;
                        continue;
                    }
                    else if(s[i+1]=='/') continue;
                }
                
                
                string tt ="";
                int j;
                for(j=i;s[j]&&s[j]!='/';j++)
                    tt+=s[j];
                tmp[p++]=tt;
                i=j;
                
            }
            if(p)
                for(int i=0;i<p;i++) cout<<"/"<<tmp[i];
            else
                cout<<"/";
        }
        else
        {
            for(int i=0;i<top;i++)cout<<"/"<<que[i];
        }
        cout<<endl;
        
    }
    return 0;
}

### CCF 2014年6月第3题:路径解析解决方案 对于给定的文件系统操作记录,目标是从这些记录中计算出当前目录下各文件和子目录的状态。此问题可以通过维护一个栈来模拟命令行中的目录切换过程。 #### 数据结构设计 为了高效处理输入指令并更新状态,可以采用如下方法: - 使用 `stack` 存储当前所在路径; - 利用字典 `dict()` 或哈希表存储每个节点的信息(包括名称、类型以及大小),其中键为完整路径字符串,值则是一个元组 `(type, size)` 表示该位置对应的实体及其尺寸;如果遇到的是文件夹,则其初始容量设为零[^1]。 #### 输入解析逻辑 针对每一条命令执行相应的动作: 当接收到 `"cd /"` 命令时,清空堆栈并将根目录压入作为起始点。 ```python if command.startswith('cd /'): stack.clear() stack.append('/') ``` 面对相对路径形式下的进入子级请求 `"cd name"` ,只需简单地把新一层加入到现有路径序列里即可完成转换工作。 ```python elif command.startswith('cd '): target = command.split()[1] if not target.startswith('/'): current_path = '/'.join(stack) + '/' + target.lstrip('/') else: current_path = target.rstrip('/') or '/' while stack and not current_path.startswith('/'.join(stack)): stack.pop() parts = current_path.strip('/').split('/') for part in parts: full_part = ('/' if not stack else '/'.join(stack)) + '/' + part if part != '' and (not stack or stack[-1] != full_part): stack.append(full_part) ``` 对于返回上层的操作 `"cd .."` , 只需弹出最顶端元素实现退回到上级父级的功能. ```python elif command == "cd ..": if len(stack)>1: stack.pop(-1) ``` 最后一种情况即创建新的文件或文件夹 `"mkdir name"` 和 `"touch filename bytesize"` 。此时应先判断目的地址是否存在再决定是否新增项目。 ```python def add_entry(path, entry_type, size=0): entries[path] = (entry_type, size) for cmd in commands: ... elif cmd.startswith("mkdir"): dir_name = "/".join(stack)+"/"+cmd.replace("mkdir ", "") add_entry(dir_name,'dir') elif cmd.startswith("touch"): file_info = cmd.replace("touch ","").rsplit(" ",1) file_name,size=file_info[0],int(file_info[1]) file_path="/".join(stack)+"/"+file_name add_entry(file_path,'file',size) ``` 通过上述方式能够有效地追踪每一次变动后的最新布局状况,并最终输出所求的结果集。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值