题解 CF20A 【BerOS file system】
对于此题,我的心近乎崩溃
这道题,注意点没有什么,相信大佬们是可以自己写出来的 我是蒟蒻,那我是怎么写出来的啊
好了,废话少说,开始进入正题
这道题,首先我想到的是字符串的 erase 函数,一边运行一边删除多余的字符
但是……
在删除的同时,串长也会变化啊!!( 绝望
所以,在直接删的情况下:
for 不能用……
while 不能用……
do while 不能用……
这里,我又想到了……
正难则反
不减掉字符,就可以再定义一个字符串 ( 空串 ) 把满足的字符加进字符串里呀!!
这样,我们又可以得出满足字符的条件:
· 上一个字符不是 ' / ' ,但是这一个字符是 ' / '
· 数字
最后,如果最后一个字符是 ' / ' ,就删去它
所以,我们得出了核心代码:
ans+=s[0];
for(i=1;i<len;i++){
if((s[i-1]!='/'&&s[i]=='/')||s[i]!='/')
ans+=s[i],k++;
}
if(ans[k]=='/')
ans.erase(k);
很简单吧?
但是,你把代码放上去测,明显地……
WA辣!
证据:

所以,我们留意到了特殊情况:
如果你输入了
/////
出来的是
nothing?
What ? 没有结果?
我瞬间认识到了:
我需要特判!!
所以,改进代码如下:
if(ans=="")//如果全是 '/' ,删完了的话
cout<<"/";
else
cout<<ans;
就这样结束了?
好像是的
所以,放出代码最终版:
#include<bits/stdc++.h>
using namespace std;//本蒟蒻总会写
int i,len,k;
string s,ans;
int main(){
getline(cin,s);
len=s.size();
ans+=s[0];
for(i=1;i<len;i++){
if((s[i-1]!='/'&&s[i]=='/')||s[i]!='/')
ans+=s[i],k++;//记下ans有多少位
}
if(ans[k]=='/')
ans.erase(k);//erase函数,温习一下
if(ans=="")
cout<<"/";
else
cout<<ans;
return 0;
}
OI 加油!洛谷冲鸭!
题解 CF20A 【BerOS file system】的更多相关文章
- Code Forces 20A BerOS file system
A. BerOS file system time limit per test 2 seconds memory limit per test 64 megabytes input standard ...
- BerOS file system
The new operating system BerOS has a nice feature. It is possible to use any number of characters '/ ...
- CDOJ_327 BerOS file system
原题地址:http://acm.uestc.edu.cn/#/problem/show/327 The new operating system BerOS has a nice feature. I ...
- BerOS File Suggestion(字符串匹配map)
BerOS File Suggestion(stl-map应用) Polycarp is working on a new operating system called BerOS. He asks ...
- Design and Implementation of the Sun Network File System
Introduction The network file system(NFS) is a client/service application that provides shared file ...
- 乌版图 read-only file system
今天在启动虚拟机的时候,运行命令svn up的时候,提示lock,并且read-only file system,这个....我是小白啊,怎么办?前辈在专心写代码,不好打扰,果断找度娘啊 于是乎,折腾 ...
- File system needs to be upgraded. You have version null and I want version 7
安装hbase时候报错: File system needs to be upgraded. You have version null and I want version 7 注: 我安装的hba ...
- Linux系统启动错误 contains a file system with errors, check forced解决方法
/dev/sda1 contains a file system with errors, check forced./dev/sda1: Inodes that were part of a cor ...
- Linux 执行partprobe命令时遇到Unable to open /dev/sr0 read-write (Read-only file system)
在使用fdisk创建分区时,我们会使用partprobe命令可以使kernel重新读取分区信息,从而避免重启系统,但是有时候会遇到下面错误信息"Warning: Unable to open ...
随机推荐
- python 变量,输入,输出
目录 2.0 注释 2.1 变量 2.2 变量名命名规范 2.3 常量 2.4 输入 input 2.5 输出 print 2.6 关于开发工具 2.0 注释 python的注释方法 "&q ...
- 如何看待 Kotlin 成为 Android 官方支持开发语言
Google IO 2017宣布了 Kotlin 会成为 Android 官方开发语言.一时间朋友圈和Android圈被各种刷屏.当然我也顺势而为发布了一篇的文章<为什么我要改用Kotlin&g ...
- 树状数组 洛谷P3616 富金森林公园
P3616 富金森林公园 题目描述 博艾的富金森林公园里有一个长长的富金山脉,山脉是由一块块巨石并列构成的,编号从1到N.每一个巨石有一个海拔高度.而这个山脉又在一个盆地中,盆地里可能会积水,积水也有 ...
- CollectionUtils.select 集合筛选
import org.apache.commons.collections.CollectionUtils;import org.apache.commons.collections.Predicat ...
- eclipse ctrl+h
之前我的eclipse使用ctrl + h 出现的总是Plug-in Search ,总是需要点那个小三角才能找到File Search ,深感使用不便, 修改方法:Window->Genera ...
- VirtualBox 在Centos 7 中安装增强功能 (共享文件夹)
1.分配光驱 2.安装相关依赖包 yum install -y bzip2 gcc gcc-devel gcc-c++ gcc-c++-devel make kernel-d 3.创建临时文件夹 mk ...
- 从左到右的滑块-Au3
说明:使用selenium自动化登陆时会弹出滑块,做人机识别,使用Au3模拟鼠标移动轨迹--可验证通过 #include <MsgBoxConstants.au3> #include &l ...
- linux安装gcc和gcc-c++
有些VPS中使用的是精简版Linux,上篇linux下自动备份blog到dropbox中的gcc和gcc-c++编译器没安装,无法编译出链接的"hostid",下面是Linux安装 ...
- eclipse配置android开发环境并搭建第一个helloWord工程
一.搭建Android在eclipse下环境 一.JDK(不用安装 下载地址: http://www.xp510.com/xiazai/Application/program/23625.ht ...
- c# string.format 的简写 $
var name = "huchao"; var info = $"你是谁,我叫:{name}"; Console.Write(info); Console.R ...