# Help Martha Solutions CENS20G

Page Contents

## Help Martha Solutions

Jonas asked Martha to remain at point (x1,y1) on an unending cartesian plane. He gave her a string S which comprises of tasks as characters ‘R’, ‘L’, ‘U’, and ‘D’.

Each character of the string relates to the accompanying developments:

‘R’ signifies moving right by 1 unit i.e, (x,y) to (x+1,y).

‘L’ signifies moving left by 1 unit i.e, (x,y) to (x−1,y).

‘U’ signifies climbing by 1 unit i.e, (x,y) to (x,y+1).

‘D’ signifies descending by 1 unit i.e, (x,y) to (x,y−1).

Presently, Jonas asks Martha Q inquiries. In each inquiry, arranges are given of structure (x2,y2). Martha needs to advise in the event that it is conceivable to move from (x1,y1) to (x2,y2) by picking an aftereffect of given string S and playing out the tasks in successive order.Starting point (x1,y1) stays same for all questions.

On the off chance that it is conceivable, print “YES” and the length of the littlest conceivable aftereffect of string, else print “NO”.

Review that string a will be an aftereffect of a string b if a can be gotten from b by cancellation of a few (conceivably zero or all) characters. For instance, for the string a=”codechef”, the accompanying strings are aftereffects: “code”, “ee”, “odchef”, “df”, “”, and others, however coming up next are not aftereffects: “ced”, “eehc”, “cofe”.

Note: Use of Fast IO is suggested.

Info:

First line will contain T, number of experiments. At that point experiments follow.

For each experiment, first line will contain string S, series of tasks.

Second line will contain facilitates x1 and y1.

Third line will contain Q, number of questions.

Q lines follow. Each line will contain arranges x2 and y2.

Yield:

For each inquiry in the event that aftereffect exists, at that point print “YES” (without statements) and length of littlest conceivable aftereffect isolated by a space in new line, else print “NO” (without cites) in new line.

Requirements

1≤T≤10

1≤|S|≤106

−108≤x1,x2,y1,y2≤108

1≤Q≤105

Test Input:

1

RLUDRLUD

1 2

2

2 3

1 – 2

Test Output:

Indeed 2

NO

Clarification:

Given String is “RLUDRLUD”, (x1,y1) is (1,2). We can move from (1,2) to (2,3) in only two activities on the off chance that we pick aftereffect as “RU” or “UR”.

Assume we pick “RU” as an aftereffect. ‘R’ signifies moving from (1,2) to (2,2). ‘U’ signifies moving from (2,2) to (2,3).

In the subsequent inquiry, we can’t move from (1,2) to (1,- 2) utilizing activities of the given string.

SOLUTION :

import java.util.*;

import java.io.*;

/* Name of the class has to be “Main” only if the class is public. */

class Codechef

{

public static void main (String[] args) throws IOException

{

BufferedWriter bw= new BufferedWriter( new OutputStreamWriter(System.out));

for(int i=0;i<t;i++)

{

int rlud[]= new int[4];

for(int j=0;j<seq.length();j++)

{

if(seq.charAt(j)==’R’)

rlud[0]++;

else if(seq.charAt(j)==’L’)

rlud[1]++;

else if(seq.charAt(j)==’U’)

rlud[2]++;

else

rlud[3]++;

}

/*for(int j=0;j<4;j++)

System.out.print(rlud[j]+” “);*/

String temp[]= new String[2];

int x1= Integer.parseInt(temp[0]);

int y1= Integer.parseInt(temp[1]);

for(int j=0;j<q;j++)

{

String possible=”YES”;

int min_len=0;

int x2= Integer.parseInt(temp[0]);

int y2= Integer.parseInt(temp[1]);

int x_diff= x2-x1;

int y_diff= y2-y1;

if(x_diff>0)

{

if(rlud[0]>=x_diff)

min_len = min_len+ x_diff;

else

possible=”NO”;

}

if(x_diff<0)

{

x_diff= -1*x_diff;

if(rlud[1]>=x_diff)

min_len= min_len+ x_diff;

else

possible=”NO”;

}

if(y_diff>0)

{

if(rlud[2]>=y_diff)

min_len= min_len+ y_diff;

else

possible=”NO”;

}

if(y_diff<0)

{

y_diff= -1*y_diff;

if(rlud[3]>=y_diff)

min_len= min_len+ y_diff;

else

possible=”NO”;

}

/*System.out.println(x_diff);

System.out.println(y_diff);*/

if(possible.equals(“YES”))

bw.write(“YES”+” “+min_len+”n”);

else

bw.write(“NO”+”n”);

}

}

bw.flush();

}

}