COMPILER - Editorial

ad-hoc
compiler
easy
editorial
may14

#21

Are these test cases correct ?

$ ./test < test.txt
6
4
0
0
2
6
4
$ cat test.txt
7
<><><><<<<>>>>
<<>>
><
><>
<><
<<>><<<<>>>>
><<>><<<>>>

#22

def check(s):
t=0,ans=0
for i=0 to N-1:
if s*==’<’: 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


#23
#include<iostream>
#include<bits/stdc++.h>
#include<string>
using namespace std;
typedef unsigned long long int ull;
#define F first
#define S second
#define nl printf("
");
#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*=='<'){
				st.push('<');
				run++;
			}
			else if(s*=='>' && !st.empty()){
				st.pop();
				run++;
			}
			else{
				
				break;
			}


		}
		cout<<run<<endl;
	}	


	return 0;
}

 //Anybody tell me whats wrong in this code..

#24

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?


#25

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


#26

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


#27

Can anyone help me with this

Code. Any suggestion is warmly welcomed,thankyou


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

using namespace std;

int main()
{
    ll t;
    cin>>t;
    while(t--)
    {
        //ll cnt=0;
       stack <char> 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*!='\0')
        {
            if(s1*=='<')
            {
                s.push(s1*);
                cnt++;
            }
            else if(s1*=='>')
            {
                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


#29
  • 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


#30

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


#31

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


#32

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


#33

someone please explain why I am getting WA : https://www.codechef.com/viewsolution/23655271


#34

Try this case:

1

<<><>>

Output should be 6


#35

Thanks got it!!! :stuck_out_tongue: :stuck_out_tongue:


#36

You assume, that max input length is 100, why?

See the second test case - http://ideone.com/Xxe1sf (answer is not 4 for sure)


#37


Can you help me where i m getting wrong
thank you so much


#38

for input

1
<<>

your code returns 2, but correct is 0


#39

Thanks a lot!


#40

Hadn’t understood the question clearly…