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;
}