COMPILER - Editorial

ad hoc means something that is not known in advance

ANswer for ><<>> should be 0

Why so ?? I can see that I have valid string of length 4 here

Read the problem statement carefully it has asked for length of longest valid ‘prefix’ , so the expression must start with ‘<’. Hope it helps!

1 Like

They have asked “Prefix”. Read question again. Even first ‘>’ gives answer 0 as there is no valid prefix. There is no need to check further.

https://www.codechef.com/viewsolution/24353005
is my code, please let me know where’s the mistake.
I tried various test cases and they display correct output. I used pair parentheses algorithm to solve it and have then multiplied the count object by 2 to get the output.

Can someone plz point out the mistake here… i do not understand y this is giving WA.

I am getting the wrong answer, plz explain to me why.

link

#include<bits/stdc++.h>
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL);

using namespace std ;


int main(void){
    stack<char> mystack;
    int t;
    int i=0,ans=0;
    string ch;
    int l;
    cin>>t;
    while(t--){
        ans=0;
        i=0;
        cin>>ch;
        l=ch.size();
        while(l--){
               // cout<<mystack.empty()<<endl;
        if(ch[i]=='<'){
            mystack.push(ch[i]);
            //cout<<"pushing"<<endl;

        }
        else if(!mystack.empty() and ch[i]=='>'){
            mystack.pop();
            //cout<<"poping"<<endl;
            ans+=2;
        }
        else if(mystack.empty() and ch[i]=='>'){
                break;
        }
        i++;
    }
    if(mystack.empty()){
        cout<<ans<<endl;
    }
    else{
            cout<<'0'<<endl;
    }
}
}

i am also trying to find same thing if you get ans plz tell me to

Can someone plz tell me why i am getting WA ! which test case i am missing ?
https://www.codechef.com/viewsolution/26554850

How my solution is wrong ?

> #include<iostream>
> #include<stack>
> #include<vector>
> using namespace std;
> 
> int main()
> {
>     int t;
>     vector<int> ans;
>     cin>>t;
>     while(t--)
>     {
>         stack<char> expr;
>         string str;
>         cin>>str;
>         int sum=0;
>         for(int i=0;i<str.length();i++)
>         {
>             if(str[i]=='<')
>                 expr.push(str[i]);
>             else if(!expr.empty())
>             {
>                 expr.pop();
>                 sum+=2;
>             }
>             else
>                 break;
>         }
>         ans.push_back(sum);
>     }
>     for(auto i=ans.begin();i!=ans.end();i++)
>         cout<<*i<<"\n";
>     return 0;
> }
>

The answer is correct. It should be 0, since there are no "lapin"s at the prefix.

1 Like

Can anyone tell what’s wrong with this code?

#include <iostream>
#include <vector>
#include <stack>
using namespace std;
#define ull unsigned long long int

int getK(vector<char> s) {
    stack<char> st;
    int k=0;
        for(int i=0; i<s.size(); i++) {
            if(s[i]=='<') {
                st.push('<');
            }
            else {  // '>'
                if(!st.empty()) {
                    st.pop();
                    k++;
                } 
                else {
                    break;
                }
            }
        }
        return k;
}

int main() {
    int T;
    cin >> T;
    while(T--) {
        string ss;
        cin >> ss;
        vector<char> s(ss.begin(),ss.end());
        int K = getK(s);
        if(K==getK(vector<char>(ss.begin()+1,ss.end()))) cout << 0 << endl;
        else cout << K*2 << endl;
    }
}

can anyone plz say why this is giving WA ?? Any particular case

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

Consider the testcase:

1
<<>
3 Likes

Kindly check why this is giving WA.
My logic : If ‘<’ increase top pointer. If ‘>’ decrease top pointer and increase length by 2 (1 for < and 1 for >)
Initial top pointer value = -1. If ‘>’ and top pointer is -1 it means no matching ‘<’ encountered…so return the current length of valid string till now.

Solution : CodeChef: Practical coding for everyone

Your solution fails on the testcase in the post above yours :slight_smile:

Yours is the only solution that I understood properly. Thanks for sharing!

I tried solving this problem with implementing concept of stack but it is giving WA. Although in my PC’s IDE it worked well.
Please once check my solution(CodeChef: Practical coding for everyone) it’s small.

Your solution fails on this testcase (the same as the last few posters in this thread ;))

1 Like