Compilers and parsers Problem Code: COMPILER

I tried in different ways to get the correct answer but EVERY TIME I GET WA, can anyone tell me where I am going wrong, I am really got stuck at this point and have no clue why my code is failing every time. Help would be much appreciated!!!

Here is my code: CodeChef: Practical coding for everyone

Try <<>

1 Like

Its giving me output as 2 which seems to be correct. I don’t know at which Test case my code is failing.
@sebastian, do you know where iam going wrong?

How it is correct?
You have to find longest prefix

1 Like

<> in <<> , Am i wrong here in understanding question? if iam wrong can you please elaborate?

You have to start from the first index

1 Like

I didnt get you, according to what i understood in a string given for eg: <<> have to search for opening and ending brackets.
or
Is it like we have to match “<” and “>” since starting index and checking how many matches with it?
for the ex you have given: <<> output should be 0?
<<>>>>><<<<> —> output should be 4?
Am i right?

You have to find correct match from the starting index. you should tell the length of the longest prefix of each of these expressions that is valid, or 0 if there's no such a prefix. Prefix starts from the first index only.

Both outputs are correct (0 and 4).

Hope this cleared your doubt

1 Like

Thanks alot!!! I understood the code wrongly and wondering for which TC it is failing.

What can I do to reduce the time limit?

int main()
{
short cases;
cin>>cases;
while (cases–)
{
string str;
cin>>str;
int count=0,i;

        for(i=0;i<str.size();i++)
        {
              if(str[0]=='>')
                    break;
              if(str[i]=='>')
              {
                    str.erase(i-1,2);       // <<>>
                    i-=2;
                    count+=2;
              }
              
        }
        if(str[0]=='<')
              cout<<0<<"\n";
        else
              cout<<count<<"\n";
  }
  
  return 0;

}