COMPILER - Editorial

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 : https://www.codechef.com/viewsolution/31247782

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(https://www.codechef.com/viewsolution/31943162) it’s small.

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

1 Like

Thank you so much sir.

1 Like

Thanks. I saw your post but forgot to reply. today i just happened to pass by and noticed it

1 Like

I think <<> should give 0 ?

Yep :slight_smile:

Solution adjusted accordingly…but still getting WA for some test case…Can you visualise any such case?
https://www.codechef.com/viewsolution/32322530.

MW it passed the <<> case

Consider the test input:

1
<><
1 Like

Thanks a lot! finally did it …https://www.codechef.com/viewsolution/32323080

1 Like

///maybe this is what you asked :slightly_smiling_face:
#include <bits/stdc++.h>
#include
#include
#define ll long long int
using namespace std;
bool func(string s,int i,int j)
{

stack<char> stk;

for(;i<=j;i++)
{
    
    if(s[i]=='<')
    stk.push(s[i]);
    else
    {   if(stk.empty()||stk.top()!='<')
        return false;
        else
        stk.pop();
        
    }
}
return stk.empty();

}
int main() {
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int t;cin>>t;
while(t–)
{
string s;
cin>>s;
stack stk;
vector v;

    for(int i=0;i<s.length();i++)
	{  
	    if(s[i]=='<')
	    {
           for(int j=i+1;j<s.length();j++)
           {
               if(s[j]=='>')
               {
                if(func(s,i,j)==true)////if(func(s,i+1,j-1)==true) 
                v.push_back(j-i+1);
               }
           }
	    }
        
    }
    if(v.size()==0)
    cout<<"0\n";
    else
    cout<<*max_element(v.begin(),v.end())<<"\n";


}

}

Can anyone please explain what is longest prefix length means it’s confusing please break it in simple terms.

Thanks

1 Like