-3

Print out the order number of opening and closing parenthesis in a string For example "((())(()))" the output should be "1 2 3 3 2 4 5 5 4 1"

means that "1st openning parenthesis, 2nd openning, 3rd openning, 3rd openning's closing parenthesis, 2nd openning's closing, 4th openning,..." and so on

I've tried using stack but have no idea what to do after the first closing parenthesis i hit

This is what i have so far

#include <bits/stdc++.h>
using namespace std;

void check(string s){
    stack<char> a;
    char ch[s.length()];
    int done = 0;
    for (char tmp : ch){
        if (tmp == '('){
            a.push(tmp);
            cout<<a.size() + done<<" ";
        }
        if(tmp == ')'){
            a.pop();
            done++;
            cout<<a.size() + done<<" ";
        }
    }
}

main(){
    string s;
    getline(cin, s);
    check(s);
}
NganCun
  • 155
  • 1
  • 4
  • 13
  • 4
    Please add your code as text. We generally don't write code for you. Instead we help you fix your broken code. [mcve] – drescherjm May 18 '19 at 12:41
  • You may pop from the stack and decrease the counter of open parenthesis as you hopefully did when pushing the `(`. These operations will also keep track in the `std::stack::size()` function, so no own counter is really needed. – πάντα ῥεῖ May 18 '19 at 12:42
  • 1
    I'm voting to close this question as off-topic because this is not a free code-writing service – Jesper Juhl May 18 '19 at 12:47
  • I was looking for ideas. Sorry about that. The code I just added is the idea i've got so far and i was stuck on how to handle things after i hit the first ")" – NganCun May 18 '19 at 12:53
  • @NganCun Please read [Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) – πάντα ῥεῖ May 18 '19 at 12:53
  • Wow i was exactly like the guy who posted it. Teacher told me to use it since it doesn't matter that much on a homework scale. Thanks for that – NganCun May 18 '19 at 12:59
  • If the line is `(()))` that third `)` will be a bit of a problem. If your input is guaranteed to be trustworthy and not malformed, no big deal. – Eljay May 18 '19 at 13:14
  • yes the input is guarenteed to be correct. It's just a counting problem not the balance the parenthesis problem – NganCun May 18 '19 at 13:20
  • I wrote an answer. If you found it useful please consider voting/accepting it! [What should I do when someone answers my question?] (https://stackoverflow.com/help/someone-answers) – AKL May 18 '19 at 14:04
  • When it encounters a `'('` the code doesn't need to push a `'('`, since that's the only thing that would get pushed on the stack. Instead, it should increment `done` and push the result. Each time it encounters a `')'` it should get the current parenthesis number from the top of the stack. – Pete Becker May 18 '19 at 18:06
  • Common guys, please be easy on us beginners! :) He just asked a question! Also since I usually don't get much votes from you advanced programmers, I do not mind to write free codes if I get some votes in exchange! :) – AKL May 18 '19 at 19:51
  • Please also check my new answer and if you found it... :) – AKL May 19 '19 at 06:35

1 Answers1

1

For what you want to achieve this code will do:

void check(const std::string & string_){
    std::vector<bool> pars;
    std::string::size_type iterator = 0;
    for(const auto character : string_){
        if (character == '('){
            pars.push_back(true);
            std::cout << (iterator = pars.size()) << "  ";
        }
        else if (character == ')'){
            while(!pars[--iterator]);
            pars[iterator] = false;
            std::cout << iterator + 1 << "  ";
        }
    }
}

The key is not to discard any element until the end! But just to mark them closed if a parenthesis get closed. And not to mention, to add a new element when a parenthesis gets open. The index in case of opening will be the number of the elements and when closing index will jump back to the last (still) open element!

For check("((())(()))"); result will be 1 2 3 3 2 4 5 5 4 1

For check("((())(()))()()"); result will be 1 2 3 3 2 4 5 5 4 1 6 6 7 7 (BTW is this behaviour OK?)

However for check("))(()))()()"); program will crash obviously because of the invalid index for the vector!

I suppose it can be fixed but I am wondering what would be the meaning of that. Are negative numbers allowed?

Still I recommend you to use some form of the following based on its clear logic :

#include <string>
void check(const std::string & string_){
    int counter = 0;
    for(const auto character : string_){
        if (character == '(') std::cout << ++counter;
        else if (character == ')') std::cout << --counter;
    }
    std::cout << std::endl;
}

By calling it check("((())(()))"); result will be 1232123210

Good luck!

AKL
  • 1,367
  • 7
  • 20
  • 2
    Unfortunately, this doesn't match the output that the question asks for. But it's not clear to me what the desired output actually means, so this is a reasonable guess at what the actual solution ought to be. – Pete Becker May 18 '19 at 13:58
  • @PeteBecker Yes you are right and thanks for the vote. I guess maybe it was simple mistake in his counting. BTW please don't take it too hard on us beginners! (too many down-votes for 1 question!) – AKL May 18 '19 at 14:08
  • @AKL unfortunately thats not the desired output. But thanks for the help. – NganCun May 18 '19 at 16:07
  • @PeteBecker i've added explaination for the desired output, hope you can help me find a solution cause i'm getting desperate... – NganCun May 18 '19 at 16:12
  • @NganCun No need to get depressed or to become desperate friend! :) I updated the code to produce your desired result! Please don't forget to vote/accept! – AKL May 18 '19 at 19:34
  • @NganCun thank you for your question friend! and also thank you for the vote and accepting it! – AKL May 19 '19 at 09:46
  • @NganCun I know that you accepted my answer but I wished you vote my answer up too! (please :) ) – AKL May 19 '19 at 10:19
  • I would but i dont have enough reputation to upvote anything. Sorry about that – NganCun May 19 '19 at 17:05
  • @NganCun OH I see . No worries my friend I just voted up your question. Now you you have enough reputation and as a result privilege to do so! Thanks in advance and sorry that I didn't vote it up sooner! – AKL May 19 '19 at 17:10
  • @NganCun I generally enjoy mathematical/computational problems. And your question gave me joy solving it. That was the main reason I voted your question up! Also after reading [Stack Overflow Isn’t Very Welcoming](https://stackoverflow.blog/2018/04/26/stack-overflow-isnt-very-welcoming-its-time-for-that-to-change/) I found my self in complete agreement. So lets not give in to the rejections that we might face here. And lets value the spirit of asking and answering questions regardless of the rejections and circumstances! – AKL May 19 '19 at 17:35