COMPILER - Editorial

While solving the problem, I had a doubt, will be grateful if answered. What should be the output for the input, “<><<<>>>”?

Can anyone help me with this code, returning WA :
My code

Your code doesn’t works on this test case:

<>><

Your output- 0
Correct output- 2

1 Like

Input
4
<<>>>><>
<><<<<>
<>>>
<<><>>

output
4
2
2
6

check these

Stack solution in python: https://ideone.com/991lTH

it is giving me wa ,can u plss help out me here
here is code:
#include <bits/stdc++.h>
using namespace std;

int main() {
int t;
cin>>t;
while(t–){
string str;
cin>>str;
class Node{
public:
char value;
int indexvalue;
};
std::stack f;

    int start=0;
    int end=0;
    int k=-1;
    for(int i=0;i<str.size();i++){
        char s = str[i];
        
        if(i == 0){
            if(s == '>'){
                break;
            }
        }
     if(s=='<'){
         Node n;
         n.value=s;
         n.indexvalue=i;
         f.push(n);
     }
     else{
         if(!f.empty()){
         Node tempnode=f.top();
         f.pop();
         int tempstart=tempnode.indexvalue;
         int tempend=i;
         int tempk=tempend-tempstart;
         if(tempk>k){
             k=tempk;
              start=tempstart;
             end=tempend;
         }
         }
        
         
     }
     
    }
    cout<<k+1<<endl;
    
}

}

why this code gives WA?
https://www.codechef.com/viewsolution/43619514

It fails on the sample input.

2 Likes

https://www.codechef.com/viewsolution/43631671
still gives me WA although I tested almost every input

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