Testing Robot Solution

Problem

Chef has bought a new robot, which will be used for delivering dishes to his customers. He started testing the robot by letting it move on a line.

Initially, the robot is placed at the coordinate x = X. Then, it should execute a sequence of N commands, described by a string S with length N. Each character of this string is either 'L' or 'R', denoting that the robot should walk one step to the left (decreasing x by 1) or to the right (increasing x by 1), respectively.

How many distinct points are visited by the robot when it has executed all the commands? A point p is visited by the robot if p is an integer and the robot's position before or after executing some command is x = p.

Input

  • The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows.
  • The first line of each test case contains two space-separated integers N and X.
  • The second line contains a single string S with length N.

Output

For each test case, print a single line containing one integer ― the number of points visited by the robot.

Constraints

  • 1 \le T \le 100
  • 1 \le N \le 100
  • |X| \le 1,000,000
  • S contains only characters 'L' and 'R'

Subtasks

Subtask #1 (100 points): original constraints

Sample 1:

Input
Output
2
6 10
RRLLLL
2 0
LL
5
3

Explanation:

Example case 1: The robot followed the path 10 \rightarrow 11 \rightarrow 12 \rightarrow 11 \rightarrow 10 \rightarrow 9 \rightarrow 8.

Example case 2: The robot followed the path 0 \rightarrow -1 \rightarrow -2.





Program :


 /* package codechef; // don't place package name! */


import java.util.*;

import java.lang.*;

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 java.lang.Exception

{

// your code goes here

Scanner sc=new Scanner(System.in);

int t=sc.nextInt();

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

{

   int n=sc.nextInt();

   int x=sc.nextInt();

   String s=sc.next();

   int countA=0,countB=0,countC=0,countD=0,ans=0;

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

   {

       char ch=s.charAt(j);

       if(ch=='R')

       {

        countA++;

        countC++;

       }

       else

       {

           countA--;

           countC--;

       }

       if(countB<countA)

       {

        ans++;

        countB=countA;

       }

       else if(countD>countC)

       {

           ans++;

           countD=countC;

       }

   }

   System.out.println(ans+1);

}

}

}


Post a Comment

0 Comments