字符串——BerOS file system

本文介绍了BerOS操作系统的特色,其允许在路径中使用多个/,并需将路径转换为最少/的正常化形式。任务是实现一个程序,将给定路径如//usr///local//nginx/sbin//简化为/usr/local/nginx/sbin。
摘要由CSDN通过智能技术生成

BerOS file system

题面翻译

题目描述

新的操作系统BerOS有一个很好的特性。可以在路径中使用任意数量的字符“/”作为分隔符,而不是使用一个传统的“/”。例如,字符串//usr///local//nginx/sbin///usr/local/nginx///sbin//是等效的。只有根目录的路径可以表示为单个字符“/”,才需要路径末尾的字符“/”(或此类字符的某些序列)。
如果路径包含尽可能少的字符数“/”,则称为normalized的路径。

您的任务是将给定路径转换为规范化形式。

输入格式

一行,仅包含小写字母和/的字符串,保证路径至少含有一个/,不超过 100 100 100 个字符。

输出格式

一行,一个字符串,表示简化后的路径。

题目描述

The new operating system BerOS has a nice feature. It is possible to use any number of characters ‘/’ as a delimiter in path instead of one traditional ‘/’. For example, strings //usr///local//nginx/sbin// and /usr/local/nginx///sbin are equivalent. The character ‘/’ (or some sequence of such characters) at the end of the path is required only in case of the path to the root directory, which can be represented as single character ‘/’.

A path called normalized if it contains the smallest possible number of characters ‘/’.

Your task is to transform a given path to the normalized form.

输入格式

The first line of the input contains only lowercase Latin letters and character ‘/’ — the path to some directory. All paths start with at least one character ‘/’. The length of the given line is no more than 100 characters, it is not empty.

输出格式

The path in normalized form.

样例 #1

样例输入 #1

//usr///local//nginx/sbin

样例输出 #1

/usr/local/nginx/sbin

思路

这道题要求我们把多余的"/"去掉,但本道题有很多细节:

  • 细节1:可能出现///,那么只要/。
  • 细节2:可能出现//usr///local//nginx/sbin//,由于路径要求,不可能最后还有/,因此我们/usr/local/nginx/sbin。

代码

#include<iostream>
#include<algorithm>

using namespace std;

int n;
string a,b;

int main(){
    cin>>a;
    
    n=a.size();
    
    bool f=false;
    for(int i=0;i<n;i++){
        if(a[i]!='/'){
            // cout<<a[i];
            b+=a[i];
            f=false;
        }else{
            if(!f){
                b+='/';
                f=true;
            }
        }
    }
    
    if(b[b.size()-1]=='/'&&b.size()>1)b.pop_back();
    cout<<b;
    
    return 0;
}
  • 23
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

green qwq

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值