Click here to Skip to main content
15,867,968 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
#include<bits/stdc++.h>
using namespace std;
void letters(string &s,int n,int &i){
if(i==n) return;
if(s[i]=='a'){
int m=i;
while(s[m-1]!='b' && m>0){
s[m-1]='a';
m--;
}
int j=i;
while(s[j+1]!='b' && j<n){
s[j+1]='a';
j++;
}
}
letters(s,n,++i);
}
int main(){
string s;
cin>>s;
int i=1;
int n=s.length();
letters(s,n,i);
int ans=count(s.begin(),s.end(),'a');
cout<<ans;
}


What I have tried:

Tried to submit this code on codeforces but it is giving partial output as result.
There are  no solutions or editorials related to this.
Please help???
Posted
Updated 31-Aug-22 11:24am
v3
Comments
CHill60 31-Aug-22 5:32am    
"accepted" for what?
Animesh _1SG20IS008 31-Aug-22 5:40am    
When tried to submit this code on codeforces it is giving partial output as result.
CPallini 31-Aug-22 6:31am    
That means your code is incorrect. You should try to locate the bug either by code inspection or providing ad hoc inputs to verify what is happening.

Quote:
while(s[j+1]!='b' && j<n){
s[j+1]='a';
j++;
}
This is a bug: when j is equal to (n-1) item s[n] (out of array bounds) is accessed.

Moreover:
  • You don't need recursion. At least you don't need recursion the way you have implemented it.
  • If you are at index i then you propagate the 'a' both backward and forward. Once you finished the forward propagation then you should consider index (i+j), instead of (i+1) (that is inefficient).
 
Share this answer
 
v6
Comments
Patrice T 31-Aug-22 17:29pm    
+5
CPallini 1-Sep-22 1:58am    
Thank you.
We can't help: we have no access to whatever standards codeforces may expect your code to meet.

Talk to them: they are the only people who can help you.
 
Share this answer
 
Problem - 1028923 - Codeforces[^]
Quote:
Please help???

It is amazing to see how your small piece of code is complicated for such a simple problem.
Using recursion is overkill and storing 'a's every where in string is unnecessary.
All you need is keep track of last 'a' and last 'b'.
C++
int letters(string &s){
    int counter= 0;
    int LastA= -2;
    int LastB= -1;
    for (int i= 0; i<s.length(); i++) {
        if (s[i]== 'a') {
            // if last time was a 'a'
            //    update counter from LastA
            //    update LastA
            // else
            //    update counter from LastB
            //    update LastA
        }
        else if (s[i]== 'b') {
            // if last time was a 'a'
            //    update counter from LastA
            //    update LastB
            // else
            //    update LastB
        }
    // At the end, check if you have a run of 'a's to finish
    }
    return counter;
}

This code is only general structure and not intended to give a full solution.
 
Share this answer
 
v4
Comments
Richard MacCutchan 1-Sep-22 3:50am    
Spelling:
    int LatsA= -2;

comparison?
        if (s[i]= 'a') {
Patrice T 1-Sep-22 7:40am    
Indeed :)
Richard MacCutchan 1-Sep-22 8:07am    
Lass ??

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900