-2

I am trying to erase particular characters using the erase() function from a string but its not working.

The question says you have to remove a substring which is either "AB" or "BB". When the substring gets deleted from the string the remaining parts of the string get concatenated and the process continues...

Here is the code:

#include <bits/stdc++.h>

#define ios ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);

#define int long long int

using namespace std;

int32_t main()
{

    ios;
    int t;
    cin>>t;

    while(t--)
    {
        string s;
        cin>>s;
        int i;
        for(i=0;i<s.length();i++)
        {
            if(s[i]=='A'&&s[i+1]=='B')
            {
                s.erase(i,i+1);
                cout<<s<<"\n";
                i=-1; // I want to start from begining therefore initializing i=-1 after i++ it becomes i=0;
            }                                        
            else if(s[i]=='B'&&s[i+1]=='B')
            {
                s.erase(i,i+1);
                cout<<s<<"\n";
                i=-1;
            }
        }
        //cout<<s.length()<<"\n";
    }
    return 0;
}

Input:

1
AABBBABBBB

The output is :

ABBABBBB

BBABBBB

BABBBB

BBBB

BBB

BB

B

But the output should be:

ABBABBBB

BABBBB

BBBB

BB

Am I doing something wrong?

  • 7
    [Don’t use `` at all](https://stackoverflow.com/questions/31816095/). Don’t use `#define int ...`, use a `typedef` or `using` alias instead. Also, your loops are going out of bounds of the string. Use a debugger to figure out why your output is not what you are expecting. – Remy Lebeau Oct 18 '20 at 09:01
  • what is the code supposed to do? – Alan Birtles Oct 18 '20 at 09:03
  • 3
    ... and done `using namespace std;`. Also, use [RAII](https://en.wikipedia.org/wiki/Resource_acquisition_is_initialization): don't predeclare `int i;`. Where did you base this on? The `#define`s are just plain weird. What's next? `#define false true`? – JHBonarius Oct 18 '20 at 09:15
  • 3
    `#define int long long int` technically is illegal, because that's a keyword and a standard header is included – Swift - Friday Pie Oct 18 '20 at 09:27
  • `#define int long long int` -- Don't do this. There is an `int64_t` type in C++ now. There is absolutely no need for this abomination of a macro. – PaulMcKenzie Oct 18 '20 at 10:12
  • What's wrong with #define? – Bhargav Reddy Oct 19 '20 at 10:41
  • `#define int long long int` means your program has undefined behaviour, i.e. the compiler can emit object code that does *absolutely anything* and by the rules of C++ that's valid. – Caleth Oct 19 '20 at 10:56
  • @chi plz correct me if i am wrong, s.length() gives the length of the string excluding the null character therefore accessing the last character will be possible only when the condition is i – Bhargav Reddy Oct 19 '20 at 11:21
  • @BhargavReddy You are right -- `s[s.length()]` since C++11 is guaranteed to return a null character. Still, you do not really need to check that: I would make the loop to stop earlier. Keep in mind that in your loop you access `s[i+1]` and `i+1` can be equal to `s.length()`, so you do access the null character at the end. – chi Oct 19 '20 at 11:43
  • @chi O Yes!!! such a silly mistake. – Bhargav Reddy Oct 19 '20 at 11:58

3 Answers3

1

You can change two s.erase(i, i + 1); statements to s.erase(i, 2);.

1
AABBBABBBB
ABBABBBB
BABBBB
BBBB
BB

And you can use C++ STL.

#include <vector>
#include <iostream>
#include <string>

int main() {

    int t{0};
    std::cin >> t;

    while (t--) {
        std::string s{};
        std::cin >> s;
        for (auto i = 0; i < s.length() && i + 1 < s.length(); i++) {
            if (s[i] == 'A' && s[i + 1] == 'B') {
                s.erase(i, 2);
                std::cout << s << std::endl;
                i = -1;
            } else if (s[i] == 'B' && s[i + 1] == 'B') {
                s.erase(i, 2);
                std::cout << s << std::endl;
                i = -1;
            }
        }
    }

    return EXIT_SUCCESS;
}
czs108
  • 151
  • 4
  • Yep. The OP wants to erase *two* characters, so the range has to have a width of 2, not 1. – David Schwartz Oct 18 '20 at 09:06
  • `i < s.length() && i + 1 < s.length()` <-- uhm... don't. And where are you using the STL = standard template library? You `include ` but never use it. `return EXIT_SUCCESS;` is C. `std::endl` flushes the buffer unnecessarily: use `'\n'` instead. No need to initialize `t` and `s`. `i = -1` goes back to the start of the string every time -> `i = std::max(-1, i-1);`. – JHBonarius Oct 18 '20 at 09:29
  • @JHBonarius Sorry `include ` is unnecessary. I mean use C++ STL header rather ``. About `t` and `s`, in my company, initializing every variable is a good style. You can see this https://dl.acm.org/doi/10.1145/3319535.3345654 and https://cwe.mitre.org/data/definitions/908.html – czs108 Oct 18 '20 at 09:32
  • That it's your company choosing guideline doesn't mean it's a good. Yes, you don't want issues with using uninitialized variables. But you also don't want unnecessary initialization. – JHBonarius Oct 18 '20 at 09:51
  • @DavidSchwartz Does erase(i,i+1) means I am deleting only one character ? If is it so then how its deleting two characters in the very first operation ? – Bhargav Reddy Oct 19 '20 at 11:30
  • @BhargavReddy It deletes only the first character in the very first operation. – David Schwartz Oct 19 '20 at 14:28
  • @DavidSchwartz but Its deleting "AB" at index 1 and 2 (0 is the 1st index). You can see it yourself. – Bhargav Reddy Oct 19 '20 at 14:37
  • @BhargavReddy The B is still there. – David Schwartz Oct 19 '20 at 14:58
1

Am I doing something wrong?

Yes, almost everything.

#include <iostream>
#include <string>
#include <regex>

int main()
{
    std::regex re("AB|BB");

    int t;
    std::cin >> t;

    while(t--)
    {
        std::string s;
        std::cin >> s;
        std::string prev;

        // do one replacement at a time, until there are no changes
        do
        {
            std::cout << s << '\n';
            prev = s;
            s = std::regex_replace(s, re, "", std::regex_constants::format_first_only);
        } while (s != prev);
    }
    return 0;
}
Caleth
  • 52,200
  • 2
  • 44
  • 75
-1

It's just a simple variation of bubble sort.

#include <iostream>
#include <string>

using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    
    int t;
    cin >> t;

    while(t > 0 && t--)
    {
        string s;
        cin >> s;

        for(size_t i = 0; i < s.length(); i++) {
            for(size_t j = 0; j < s.length() - 1; j++) {
                if((s[j]=='A' || s[j] == 'B') && s[j+1]=='B')
                {
                    s.erase(j,2);
                    cout<<s<<"\n";
                    break;
                }      
            }
        }
    }
    return 0;
}
Aniket Kariya
  • 1,471
  • 2
  • 21
  • 26