COMPILER AND PARSER

Contest Code:[LRNDSA02] https://www.codechef.com/LRNDSA02/problems/COMPILER

Why am i getting wrong answer?

#include <iostream>
#include<stack>
#include<algorithm>
using namespace std;

int main() {
    int t;
    cin>>t;
    while(t--)
    {
        string str;
        cin>>str;
        
        int result=0;
        stack<int > s;
        s.push(-1);
        for(int i=0;i<str.length();i++)
        { if(str[i]=='<')
        s.push(i);
        else
        { s.pop();
         if(!s.empty())
         result=max(i-s.top(),result);
         else
         s.push(i);
             
        }
            
        }
        
       
        
        cout<<result<<endl;
        
    
    }
	// your code goes here
	return 0;
}

can you please tell me what wrong with this code. i have used the logic from the problem length of longest valid substring

this test case works with my code

On my machine, your solution gives:

2

for the test input:

1
<<>

whereas it should give 0:

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

i am not understanding the question.
for input <<> the ans should be 2 as it has a valid length of 2?

No - none of the prefixes of <<> are valid (see the table above), so the answer is 0.

1 Like