COMPILER - Editorial

please tell me why i am getting wa ;(

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

int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t;
cin>>t;
while(t–)
{
string s;
cin>>s;
long n=s.size();
stack st;
long count=0,max=0;
for(long i=0;i<n;i++)
{
if(s[i]==’<’)
st.push(s[i]);
else if(st.empty())
st.push(s[i]);
else
{
if(s[i]==’>’ and st.top()==’<’)
{
st.pop();
count=count+2;
}
else if(s[i]==’>’ and st.top()!=’<’)
count=0;
else
{
st.push(s[i]);
}
}
if(count>max) max =count;
}
// long st_size=st.size();
// long prefix=n-st_size;
// cout<<prefix<<"\n";
cout<<max<<"\n";
}

// your code goes here
return 0;

}

please help me out to debug it

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

okay

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

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	int t;
	cin>>t;
	while(t--)
	{
	  string s;
	  cin>>s;
	  long n=s.size();
	  stack<char> st;
	  long count=0,max=0;
	  for(long i=0;i<n;i++)
	  {   
	      if(s[i]=='<')
	      st.push(s[i]);
	      else if(st.empty())
	      st.push(s[i]);
	      else
	      {
	          if(s[i]=='>' and st.top()=='<')
	          {
	              st.pop();
	              count=count+2;
	          }
	          else if(s[i]=='>' and st.top()!='<')
	          count=0;
	          else
	          {
	              st.push(s[i]);
	          }
	      }
	      if(count>max) max =count;
	  }
// 	  long st_size=st.size();
// 	  long prefix=n-st_size;
// 	  cout<<prefix<<"\n";
    cout<<max<<"\n";
	}
	    
	// your code goes here
	return 0;
}
1 Like

thanks for reply mate :slight_smile:

my all public test cases are passed but onSumitting it showing WA. can anyone tell me what’s wrong with by code ?
https://www.codechef.com/viewsolution/45277258

Consider the test input:

1
<

PLEASE HELP TO Figure out why it is not is not passing testcases?

#include<bits/stdc++.h>
using namespace std;
int top=-1;
void push(char e,int *st)
{
	top=top+1;
	st[top]=e;
	//cout<<e<<endl;
}
int pop(int c)
{
    if(top==-1)
    {
        return 0;
    }
    else
    {
	top=top-1;
	c=c+2;
	return c;
    }
}
int main()
{
    /*
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);   
    */
int t;
cin>>t;
while(t--)
{
	string s;
	cin>>s;
	int stack[10000];
	int i,j,l,c=0,k=0,x=0;
	l=s.length();
	for(i=0;i<l;i++)
	{
		if(s[0]=='>')
		{
			x=1;
			break;
		}
		else 
		{
		if(s[i]=='<')
		{
			push('<',stack);
		}
		else
		{
		    j=pop(c);
			k=k+j;
	//	cout<<k<<endl;
		}
		}
	}
	if(top>=0)
	{
	    cout<<'0'<<endl;
	}
	else
	{
	if(x==1)
	{
	    
	  cout<<'0'<<"\n";
	}
	else
	{
	    cout<<k<<endl;
	}
	top=-1;
	}
}
}

> Blockquote

why is this not working?

a = int(input())
def solve(t):
        c=0
        d = ['<']

        stack = []

        for i in range(len(t)):
            if t[i] == d[0]:
                #left
                stack.append('<')
            else:
                #right
                if len(stack) == 0:
                    continue
                d = stack.pop()
                c+=2

        return c                





    for i in range(a):
        b = input()
        print(solve(b))

yes it will give answer 2. That is what question is asking right? To find the number of brackets involved in the valid pairs? right?

The question asks for the length of the longest valid prefix of the given string, or 0 if there is no such prefix. None of the prefixes of <<> are valid:

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

Oh okay. Thanks. I thought they were just trying to trick us in the name of prefix :sweat_smile:.

1 Like

Can this be classified as a Sliding Window Problem?

https://www.codechef.com/viewsolution/54931923

Gone through all the test cases , printing prefix length only but getting WA. :frowning:

@admin
Suggestion: Instead of showing Accepted or WA, show us the percentage of test cases passed and percentage of test cases not passed. It is very disheartening to see WA and it is very demotivating.

Consider the test input:

1
<><>

See this test case carefully

<><
don’t count the inner brackets and don’t output 2 the o/p will be 0. Since, the outer brakets are not balanced we cann’t compare inner brackets therefore once t becomes -ve just break the loop