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?100?characters, it is not empty.
Output
The path in normalized form.
Sample input and output
| //usr///local//nginx/sbin | /usr/local/nginx/sbin |
此題可以使用常規(guī)思路,利用開(kāi)關(guān)變量,不斷判斷是否為字母,然后整個(gè)單詞輸出。但是,巧妙利用C++的流處理,能非常簡(jiǎn)單的處理這道題。 首先 ,需要聲明庫(kù)<sstream>,這是處理字符串流的庫(kù)。然后創(chuàng)建一個(gè)輸入流isstream is(s)(注意,這里的輸入并非指從鍵盤(pán)敲入,而是從字符串中 輸入),其中s是題中的字符串。接下來(lái),就將is當(dāng)作cin用,就可以啦。當(dāng)然,需要處理一下剛開(kāi)始的字符串,將所有/替換為空格。且需要注意的是 ,有一種特例是全為/,這種情況只需判斷一下是否輸出即可。 獻(xiàn)上代碼: #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);//創(chuàng)建輸入流bool flag = 0;//判斷是否有輸入while (is >> temp){cout << '/' << temp; flag = 1;}if (!flag)cout << '/';cout << endl;}return 0; }
轉(zhuǎn)載于:https://www.cnblogs.com/HarryGuo2012/p/4524051.html
總結(jié)
以上是生活随笔為你收集整理的CDOJ_327 BerOS file system的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: CSS基本知识1-CSS基本概念
- 下一篇: subShell与代码块