COMPILER - Editorial

def check(s):
t=0,ans=0
for i=0 to N-1:
if s[i]==’<’: t++;
else:
t–;
//Now, if t=0, means, the string s[0,i] is valid.
if t==0: ans=max(ans,i+1) ------> I can see there is no use of max function here as i always more than max every time t==0, so simple ans=i+1 is sufficient.
else if t<0: break //string s whole is invalid.
print ans

#include
#include<bits/stdc++.h>
#include
using namespace std;
typedef unsigned long long int ull;
#define F first
#define S second
#define nl printf("\n");
#define pb push_back
#define mp make_pair
#define f(i,a,b) for(int i=a;i<b;i++)
#define MOD 1000000007
#define fastscan ios_base::sync_with_stdio(0); cin.tie(NULL);

int main(int argc, char const *argv[])
{

long int t;
cin>>t;

while(t--){
	string s;
	cin>>s;
	ull len=s.length();
	stack<char> st;
	ull run=0;
	for (ull i = 0; i < len; ++i)
	{
		if(s[i]=='<'){
			st.push('<');
			run++;
		}
		else if(s[i]=='>' && !st.empty()){
			st.pop();
			run++;
		}
		else{
			
			break;
		}


	}
	cout<<run<<endl;
}	


return 0;

}

//Anybody tell me whats wrong in this code…

I have implemented this algorithm in Go and tried to run it but getting a time limit exceed error. But the same algorithm I’ve implimented in Python3 and run it and it accepted. Is Go compiler not well implemented?

Could anyone please tell me what I am missing in my code? Getting WA. Have attached my own test cases.

Can anyone tell me what is wrong with this solution? https://ideone.com/O4w7qo

Can anyone help me with this


[1]. Any suggestion is warmly welcomed,thankyou


  [1]: https://www.codechef.com/viewsolution/18731588

#include<bits/stdc++.h>
#define ll long long int

using namespace std;

int main()
{
ll t;
cin>>t;
while(t–)
{
//ll cnt=0;
stack s;
string s1;
cin>>s1;
ll n=s1.size();
if(n==1||s1[0]==’>’)
{
cout<<0<<endl;
continue;
}
int i=0;
ll cnt=0,res=0;
while(s1[i]!=’\0’)
{
if(s1[i]==’<’)
{
s.push(s1[i]);
cnt++;
}
else if(s1[i]==’>’)
{
s.pop();
}
if(s.empty())
{
res+=cnt;
cnt=0;
}
i++;
}
cout<<res*2<<endl;

}
return 0;

}

what is the prob with my code ??
i have checked most of the testacases all are passing still am getting wa.
please help me with testcases

  • Solved it using stack
  • Take a counter c=0 and a summing variable like sum=0
  • when ‘<’ comes push into stack
  • When ‘>’ comes, check when a pair closes(means top of stack should be ‘<’) increment the counter and after that pop one’<’ from stack and check if stack is empty add 2*c to the sum
  • also check if the stack is already empty and ‘>’ comes break your loop of string and print sum

Rest is your implementation of code you can check my code
link text

both author and setter has wrong ans.
Check for “>><>” this.
answer should be 2.
but your solution shows, 0.

Can anybody tell me where the mistake is in this code. I’m getting WA

can someone please tell me what’s wrong and why it does not get submitted successfully when it gives correct answer to the test case given in the problem.
code : https://ideone.com/H6AA9w