CDOJ_327 BerOS file system
原题地址:http://acm.uestc.edu.cn/#/problem/show/327
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
There are multi-cases. The first line of each case 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
Output
The path in normalized form.
Sample input and output
| Sample Input | Sample Output |
|---|---|
//usr///local//nginx/sbin |
/usr/local/nginx/sbin |
<sstream>,这是处理字符串流的库。然后创建一个输入流isstream
is(s)(注意,这里的输入并非指从键盘敲入,而是从字符串中/替换为空格。且需要注意的是/,这种情况只需判断一下是否输出即可。
献上代码:#include<iostream>
#include<string>
#include<sstream>
using namespace std;
int main()
{
string s, temp;
while (cin >> s)
{
for (int i = 0; i < s.length(); i++)
if (s[i] == '/')
s.replace(i, 1, 1, ' ');//将斜杠代换为空格
istringstream is(s);//创建输入流
bool flag = 0;//判断是否有输入
while (is >> temp)
{
cout << '/' << temp;
flag = 1;
}
if (!flag)
cout << '/';
cout << endl;
}
return 0;
}
浙公网安备 33010602011771号