CENS20G - Editorial

pls can someone tell me whats wrong with my code

#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
int main()
{
    ll i,j,c,t,x1,x2,y1,y2,q;
    char ch;
    string s,str;
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin>>t;
    while(t--)
    {
        cin>>s;
        cin>>x1>>y1;
        cin>>q;
        while(q--)
        {
            cin>>x2>>y2;
            str="";c=0;
            if(x1>x2)
            ch='L';
            else
            ch='R';
            for(i=0;i<abs(x1-x2);i++)
            str+=ch;//creating the required subsequence 
            if(y1>y2)
            ch='D';
            else
            ch='U';
            for(i=0;i<abs(y1-y2);i++)
            str+=ch;//here also creating the required subsequence
            j=0;
            for(i=0;i<str.length();i++)//checking if the subsequence is present in the string or not
            {
                while(j<s.length())
                {
                    if(str[i]==s[j])
                    {
                        c++;j++;break;//maintaining the count c if every character of the subsequence is present or not in the string
                    }
                    j++;
                }
            }
            if(c==str.length())
            cout<<"YES "<<c<<"\n";
            else
            cout<<"NO\n";

        }
    }

}

#include<bits/stdc++.h>
using namespace std;
#define ll long long

#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

int main()
{
IOS;
ll t; cin>>t;
string s; cin>>s;
ll r1 =0, l1=0,u1=0,d1=0;
for(int i=0;i<s.length();i++)
{
if(s[i]==‘L’) l1++;
if(s[i]==‘R’) r1++;
if(s[i]==‘U’) u1++;
if(s[i]==‘D’) d1++;
}
while(t–)
{
ll x,y,l,r,u,d; cin>>x>>y;

	ll n;
	cin>>n;
	while(n--)
	{
		ll x1,y1; cin>>x1>>y1;
		l =l1; r=r1; d=d1; u=u1;
		ll diffx = x1 - x;//>r
		ll diffy = y1 - y;//>u
		if(diffx<0)
		{
			l+=diffx;
		}
		else if(diffx>0)
		{
			r-=diffx;
		}
		if(diffy>0)
		{
			u-=diffy;
		}
		else if(diffy<0)
		{
			d+=diffy;
		}
		
		if(l>=0 and r>=0 and u>=0 and d>=0)
		{
			cout<<"YES"<<" "<<abs(diffx)+abs(diffy)<<endl;
		}
		else
		{
			cout<<"NO"<<endl;
		}
	}
	
}

}

What’s wrong with this?
Please help!

That works…

I did the same thing but got wrong answer can anyone please tell me where I was wrong?
(There is no issue with uppercase lowercase yes no)
https://www.codechef.com/viewsolution/37001346

Consider the test input:

1
DDDD
1 2
1
1 3
2 Likes

thanks brother

I did the exact same thing but still got wrong answer.
https://www.codechef.com/viewsolution/37002865
Can you help me figure out what I missed …
Thank You

Consider the test input:

1
R
0 0
1
100 1
1 Like

I had wrongly interpreted the instructions, I was taking y1+1 for D instead for U. But after that I still received a TLE, I was using fast i/o and ‘\n’ but still was getting a TLE. After that instead of using an array for storing the character counts I switched to using 4 individual variables and then got accepted. Can anyone explain why I was getting a TLE with an array?

yeah bro thanks man , i got my mistake ! :slight_smile:

I ran my code using custom input same as given in the problem statement and it ran successfully.
But on submitting I was shown that my answer is wrong. Can anyone please help me out in finding out what went wrong in my code??
https://www.codechef.com/viewsolution/37020739

It doesn’t give the required output, though:

[simon@simon-laptop][13:52:51]
[~/devel/hackerrank/otherpeoples]>echo "1
RLUDRLUD
1 2
2
2 3
1 -2" | ./harry_8761-CENS20G.py
YES 2

NO

(note the extra blank line).

1 Like

Brother I tried to make the changes u suggested …still the same response i.e. “Wrong Answer”

Can you give the link to your new submission?

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

1 Like

CENS20G - Editorial - #72 by ssjgz :slight_smile:

I got the same error even after using FAST IO, So, i changed the endl with \n and it worked!!

2 Likes

my code blocks ide is not showing output when i am using “\n” but showing output when i am using endl for this problem. But using “\n” gave me correct answer now when submitted ,can anyone help me rectify this.

There are several differences between your two submissions besides the use of "\n" vs endl:

https://www.diffchecker.com/GPBAPoj6

with the most important one being this:

m.count('U')>=l

:slight_smile:

Edit: As for Codeblocks - I’m guessing it’s just not doing a final flush(?) A bit weird, but oh well.

Having

cout.flush();

as the last statement executed should do the trick, I guess.

2 Likes

yeah i got the point using map, then it turned tle so i tried using multiset.

yeah this worked out ,thanks for the reply.

1 Like