Solution to a problem CF20A [BerOS file system]

For this question, my heart almost collapsed

This question, do not pay attention to what point, I believe bigwigs could be written out of their own I am konjac, that is how I write out ah

Well, without further ado, we began to enter the topic

This question, first of all I am thinking of the string erase function, while running side to remove extra characters

but……

At the same time to delete, string length will change ah! ! (despair

Therefore, in the case of direct deleted:

can not be used for ......

while not use ......

can not do while ......

Here, I thought ......

Zhengnanzefan

Character is not lost, can then define a string (empty string) satisfies the character string added in it! !

In this way, we can draw the character of conditions are met:

· The character is not a '/', but this character is '/'

· Digital

Finally, if the last character is '/', then deleting it

So, we come to the core code:

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);

Simple, right?

However, the code you put up measure, obviously ......

WA 辣!

evidence:

So, we note that the special circumstances:

If you enter

/////

Out of the

nothing?

What? No results?

I instantly recognized:

I need a special judge! !

Therefore, an improved code is as follows:

if(ans=="")//如果全是 '/' ,删完了的话
    cout<<"/"; else cout<<ans;

This is the end?

seems like it

Therefore, the release of the final version of the code:

#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 Come on! Luoguchongya!

Guess you like

Origin www.cnblogs.com/SeashellBaylor/p/11079810.html