thinking:用for循环每个字符遇到第一个ogo,将其三个位置改为***,然后i+2,(然后此时的i与i+2是否为"g""o",如果是i又加2)把括号内的循环,在这句结束时,I--,是因为这句话完,
因为for的循环,i又加一次,这样是为了抵消.
failure:1、用循环将后面的值改变时,需要考虑循环到后面时这个值该如何改变。
2、把思路理清,新别太着急,认真一些
Polycarp has interviewed Oleg and has written the interview down without punctuation marks and spaces to save time. Thus, the interview is now a string s consisting of n lowercase English letters.
There is a filler word ogo in Oleg's speech. All words that can be obtained from ogo by adding go several times to the end of it are also considered to be fillers. For example, the words ogo, ogogo, ogogogo are fillers, but the words go, og, ogog, ogogog and oggo are not fillers.
The fillers have maximal size, for example, for ogogoo speech we can't consider ogo a filler and goo as a normal phrase. We should consider ogogo as a filler here.
To print the interview, Polycarp has to replace each of the fillers with three asterisks. Note that a filler word is replaced with exactly three asterisks regardless of its length.
Polycarp has dealt with this problem in no time. Can you do the same? The clock is ticking!
The first line contains a positive integer n (1 ≤ n ≤ 100) — the length of the interview.
The second line contains the string s of length n, consisting of lowercase English letters.
Print the interview text after the replacement of each of the fillers with "***". It is allowed for the substring "***" to have several consecutive occurences.
7 aogogob
a***b
13 ogogmgogogogo
***gmg***
9 ogoogoogo
*********
The first sample contains one filler word ogogo, so the interview for printing is "a***b".
The second sample contains two fillers ogo and ogogogo. Thus, the interview is transformed to "***gmg***".
代码:
#include <bits/stdc++.h>
using namespace std;
int main(){
char a[210];
char b[210];
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
int n;
cin >> n;
cin >> a;
int length=strlen(a);
int j=0;
for(int i=0;i<length;i++){
if(a[i]=='g'&&a[i-1]=='o'&&a[i+1]=='o'){
b[i-1]='*';
b[i]='*';
b[i+1]='*';
i=i+2;
while(a[i]=='g'&&a[i+1]=='o'){
i=i+2;
}
i=i-1;
}else{
b[i]=a[i];
}
}
for(j=0;j<=length;j++)
if(b[j]!=0){
cout << b[j];}
}