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 . Then, it should execute a sequence of commands, described by a string with length . Each character of this string is either 'L' or 'R', denoting that the robot should walk one step to the left (decreasing by ) or to the right (increasing by ), respectively.
How many distinct points are visited by the robot when it has executed all the commands? A point is visited by the robot if is an integer and the robot's position before or after executing some command is .
Input
- The first line of the input contains a single integer denoting the number of test cases. The description of test cases follows.
- The first line of each test case contains two space-separated integers and .
- The second line contains a single string with length .
Output
For each test case, print a single line containing one integer ― the number of points visited by the robot.
Constraints
- contains only characters 'L' and 'R'
Subtasks
Subtask #1 (100 points): original constraints
Sample 1:
2 6 10 RRLLLL 2 0 LL
5 3
Explanation:
Example case 1: The robot followed the path .
Example case 2: The robot followed the path .
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);
}
}
}
0 Comments