Compilers and parsers

What is the bug in my solution? question

#include<iostream>
using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        string s;
        cin>>s;
        bool found = 0;
        int length = 0, count = 0, opened=0,valid = 0;
        int xmax = 0;


        for(int i=0; i<s.size(); ++i)
        {
            if(opened==0 && s[i]=='>')
            {
                break;
            }
            if(s[i]=='<')
            {
                found = 1;
                opened++;
            }
            if(found && opened>0)
            {
                if(s[i]=='>')
                {
                    length+=2;
                    opened--;
                }
            }
            if(opened==0)
            {
                count+=length;
                xmax = max(xmax,count);
                found =0;
                length=0;
                valid = 1;
            }
        }
        if(valid)
        cout<<xmax<<endl;
        else
            cout<<0<<endl;
    }
}


Just give me a counter example and I’ll sort it myself

Please either format your code or (better!) link to your submission - the forum software has mangled it and it won’t compile! :slight_smile:

2 Likes

@ssjgz Done

1 Like

Thanks - it’s this one again: COMPILER - Editorial

1 Like

I didn’t understand, how is <>>> a valid input (answer 2) whereas <<> is not? How are we dealing this?

Anyone? : [

For <<>:

prefixLength: prefix:  is valid: 
1             <        false
2             <<       false
3             <<>      false
==========
0

For <>>>:

prefixLength: prefix:  is valid: 
1             <        false
2             <>       true
3             <>>      false
4             <>>>     false
==========
2
2 Likes

@ssjgz what’s wrong now?! I have edited the code, added a valid flag which is set to true as soon as a closing bracket pair is found. Thanks

1 Like

Consider:

1
<><>
2 Likes

what now? xx

1
<>><>
2 Likes

Thanks very much

1 Like

What now?

1
<>><><>

:slight_smile:

2 Likes

so it’s output will be 2?

The output needs to be 2 for that test input, yes :slight_smile:

what about <><<<>> ?

prefixLength: prefix:  is valid: 
1             <        false
2             <>       true
3             <><      false
4             <><<     false
5             <><<<    false
6             <><<<>   false
7             <><<<>>  false
==========
2

1 Like

what now?