BerOS file system
CodeForces - 20AThe 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.
//usr///local//nginx/sbin
/usr/local/nginx/sbin
code:
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
char str[1100];
int main(){
while(~scanf("%s",str)){
int len = strlen(str);
for(int i = 0; i < len; i++){
if(str[i] == '/'){
i++;
while(i < len && str[i] == '/'){
str[i] = '.';
i++;
}
i--;
}
}
int k = 0;
char ans[1000];
for(int i = 0; i < len; i++){
if(str[i] != '.'){
ans[k++] = str[i];
}
}
while(k > 0 && ans[k-1] == '/') k--;
ans[k] = 0;
if(k == 0) printf("/\n");
else printf("%s\n",ans);
}
return 0;
}