【水I/O】#20 A. BerOS file system

本文介绍了一个简单的路径规范化问题,即如何将包含多个斜杠分隔符的路径转换为最简形式。通过几个C++代码示例展示了不同的实现方法,并讨论了在处理过程中遇到的一些常见问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

Sample test(s)
input
//usr///local//nginx/sbin
output
/usr/local/nginx/sbin

——————————————————————————


《Updated 2015/07/15》

回头来看了看这题,觉得以前写的还是略显凌乱,写了一个用STL-String的

AC-Code:

#include <cstdio>  
#include <string>
#include <cstring>  
#include <iostream>  
//http://blog.youkuaiyun.com/okcd00/article/details/27373475
using namespace std;  
  
int main()   
{
	string s,ans=""; 
	cin>>s; 
	int len=s.length();
	while(s[len-1]=='/')len--;
	s=s.substr(0,len);
	for(int i=0;i<len;i++)
	{
		if(s[i]!='/') ans=ans+s[i];
		else if(i+1<len && s[i+1]!='/') ans=ans+s[i];
	}
	if(ans.length()==0)cout<<'/'<<endl;
	else cout<<ans<<endl;
	return 0;  
}  


——————————————————————————

看到这道题,吾辈欣喜了一小会~,哇咔咔,水题,秒~

然后写了这个:

#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

int main()
{
	bool flag=true;
	char tmp;
	while(scanf("%c",&tmp)!=EOF)
	{
		if(tmp=='/')flag=true;
		else if(tmp!='/'&&flag==true)
		{
			printf("/%c",tmp);
			flag=false;
		}
		else if(tmp!='/'&&flag==false)printf("%c",tmp);
	}
	return 0;
} 

然后……WA了…… 原因是///a//这样的case输出了/a/ 多了一个结尾的/,百思不得其解

然后,

wrong output format Unexpected end of file - token expected

再然后:

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

int main() 
{
	char str[1000];
  	while(scanf("%s", str) != EOF) //get the whole line
	{
    	int len = strlen(str);		//length
    	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';//end
    	if(k == 0) printf("/\n");
    	else printf("%s\n", ans);
  	}
  return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

糖果天王

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值