0%

CF20A题解

CF20A [BerOS file system]

原题

A. BerOS file system

time limit per test: 2 seconds

memory limit per test: 64 megabytes

input: standard input

output: standard output

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.

Input

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.

Output

The path in normalized form.

Examples

input
1
//usr///local//nginx/sbin
output
1
/usr/local/nginx/sbin

题解

意思差不多就是要把这个给你的字符串里面重复的/删去 例子很直观了

一道简单的不能再简单的红题 让我做出了花样

很多用word等软件给文档去空行过的同学们肯定印象深刻 把^p^p 替换成^p 多点两次就行了 这里也可以用到这种思想 (word中^p就是换行的意思)

替换 就是replace 查了cppreference 发现还真有这么个 std::string::replace 不过不像C#的string.replace(char, char)那么简单 这个std::string::replace(int pos, int count, string str)是 把string中从pos开始的count个字符替换成str

替换

其实也简单 于是就有了

1
a = a.replace(a.find("//", 0), 2, "/"); //把 //(两个字符)替换成/

重复

但是回想去空行的时候 如果有连续的空行 就要多点两次

1
2
3
4
L1:
a = a.replace(a.find("//", 0), 2, "/");
if (a.find("//", 0) != string::npos) //string::npos就是找不到了
goto L1; //别骂我用goto了QAQ

空路径特判

然后RE了 因为题目中说

All paths start with at least one character ‘/’.

所以因为可能路径中并没有// 这样子替换就会RE

于是

1
2
3
4
5
6
L1:
if (a.find("//", 0) == string::npos)
goto L2;
a = a.replace(a.find("//", 0), 2, "/");
goto L1;
L2:

根目录特判

然后WA了

因为如果整个路径就一个/ 那就会被去掉 于是

1
2
3
4
5
6
7
8
L1:
if (a.find("//", 0) == string::npos)
goto L2;
a = a.replace(a.find("//", 0), 2, "/");
goto L1;
L2:
if (a[a.length() - 1] == '/' && a.length() != 1) //加了个if
a = a.substr(0, a.length() - 1);

终于AC了QAQ

完整代码

完整代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include<bits/stdc++.h>
using namespace std;

int main()
{
string a;
cin >> a;
L1:
if (a.find("//", 0) == string::npos)
goto L2;
a = a.replace(a.find("//", 0), 2, "/");
goto L1;
L2:
if (a[a.length() - 1] == '/' && a.length() != 1)
a = a.substr(0, a.length() - 1);
cout << a;
return 0;
}