L - One-Dimensional Maze

Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu

Description

BaoBao is trapped in a one-dimensional maze consisting of \(n\) grids arranged in a row! The grids are numbered from 1 to \(n\) from left to right, and the \(i\)-th grid is marked with a character \(s_i\), where \(s_i\) is either 'L' or 'R'.

Starting from the \(m\)-th grid, BaoBao will repeatedly take the following steps until he escapes the maze:

  • If BaoBao is in the 1st grid or the \(n\)-th grid, then BaoBao is considered to arrive at the exit and thus can escape successfully.
  • Otherwise, let BaoBao be in the \(t\)-th grid. If \(s_t = \text{'L'}\), BaoBao will move to the \((t-1)\)-th grid; If \(s_t = \text{'R'}\), Baobao will move to the \((t+1)\)-th grid.

Before taking the above steps, BaoBao can change the characters in some grids to help himself escape. Concretely speaking, for the \(i\)-th grid, BaoBao can change \(s_i\) from 'L' to 'R', or from 'R' to 'L'.

But changing characters in grids is a tiring job. Your task is to help BaoBao calculate the minimum number of grids he has to change to escape the maze.

Input

There are multiple test cases. The first line of the input contains an integer \(T\), indicating the number of test cases. For each test case:

The first line contains two integers \(n\) and \(m\) (\(3 \le n \le 10^5\), \(1 < m < n\)), indicating the number of grids in the maze, and the index of the starting grid.

The second line contains a string \(s\) (\(|s| = n\)) consisting of characters 'L' and 'R'. The \(i\)-th character of \(s\) indicates the character in the \(i\)-th grid.

It is guaranteed that the sum of \(n\) over all test cases will not exceed \(10^6\).

Output

For each test case output one line containing one integer, indicating the minimum number of grids BaoBao has to change to escape the maze.

Sample Input

3
3 2
LRL
10 4
RRRRRRRLLR
7 4
RLLRLLR

Sample Output

0
2
1

Hint

For the first sample test case, BaoBao doesn't have to change any character and can escape from the 3rd grid. So the answer is 0.

For the second sample test case, BaoBao can change \(s_8\) to 'R' and \(s_9\) to 'R' and escape from the 10th grid. So the answer is 2.

For the third sample test case, BaoBao can change \(s_4\) to 'L' and escape from the 1st grid. So the answer is 1.

题意:给出一串只有RL的字符串,
给定起始位置,R表示向右,L表示向左,
现在要求输出最少改变几个字符可以移动到字符第一个或者最后一个。
例如:
6 3
LLRRLR
起始位置是第三个字符R,只用改变第三个R就可以到达第一个字符L
这道题目通过模拟就可以做出来,
就是将到达两边要改变的字符都求出来,输出最小的那个

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define max_v 100005
char str[max_v];
/*
Submit Failed
*/
int main()
{
int t;
int n,m,sum1,sum2;
scanf("%d",&t);
while(t--)
{
scanf("%d %d",&n,&m);
scanf("%s",str+);
sum1=;
sum2=;
for(int i=m;i<n;i++)
if(str[i]=='L')
sum1++;
for(int i=m;i>;i--)
if(str[i]=='R')
sum2++;
printf("%d\n",min(sum1,sum2));
}
return ;
}
/*
题意:给出一串只有RL的字符串,
给定起始位置,R表示向右,L表示向左,
现在要求输出最少改变几个字符可以移动到字符第一个或者最后一个。
例如:
6 3
LLRRLR 起始位置是第三个字符R,只用改变第三个R就可以到达第一个字符L
这道题目通过模拟就可以做出来,
就是将到达两边要改变的字符都求出来,输出最小的那个 */
 

ZOJ 3992 One-Dimensional Maze(思维题)的更多相关文章

  1. ZOJ 3983 Crusaders Quest(思维题)

    C - Crusaders Quest Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu S ...

  2. zoj 3778 Talented Chef(思维题)

    题目 题意:一个人可以在一分钟同时进行m道菜的一个步骤,共有n道菜,每道菜各有xi个步骤,求做完的最短时间. 思路:一道很水的思维题, 根本不需要去 考虑模拟过程 以及先做那道菜(比赛的时候就是这么考 ...

  3. ZOJ 3829 贪心 思维题

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3829 现场做这道题的时候,感觉是思维题.自己智商不够.不敢搞,想着队友智商 ...

  4. cf A. Inna and Pink Pony(思维题)

    题目:http://codeforces.com/contest/374/problem/A 题意:求到达边界的最小步数.. 刚开始以为是 bfs,不过数据10^6太大了,肯定不是... 一个思维题, ...

  5. 洛谷P4643 [国家集训队]阿狸和桃子的游戏(思维题+贪心)

    思维题,好题 把每条边的边权平分到这条边的两个顶点上,之后就是个sb贪心了 正确性证明: 如果一条边的两个顶点被一个人选了,一整条边的贡献就凑齐了 如果分别被两个人选了,一作差就抵消了,相当于谁都没有 ...

  6. C. Nice Garland Codeforces Round #535 (Div. 3) 思维题

    C. Nice Garland time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...

  7. PJ考试可能会用到的数学思维题选讲-自学教程-自学笔记

    PJ考试可能会用到的数学思维题选讲 by Pleiades_Antares 是学弟学妹的讲义--然后一部分题目是我弄的一部分来源于洛谷用户@ 普及组的一些数学思维题,所以可能有点菜咯别怪我 OI中的数 ...

  8. UVA 1394 And Then There Was One / Gym 101415A And Then There Was One / UVAlive 3882 And Then There Was One / POJ 3517 And Then There Was One / Aizu 1275 And Then There Was One (动态规划,思维题)

    UVA 1394 And Then There Was One / Gym 101415A And Then There Was One / UVAlive 3882 And Then There W ...

  9. HDU 1029 Ignatius and the Princess IV / HYSBZ(BZOJ) 2456 mode(思维题,~~排序?~~)

    HDU 1029 Ignatius and the Princess IV (思维题,排序?) Description "OK, you are not too bad, em... But ...

随机推荐

  1. js和jquery中获取非行间样式

    样式又分为了行间样式和非行间样式.一般来说行间样式用的是比较少的,因为它能够作用的范围就只有一个元素,而非行间样式的作用范围可以是一类元素(即拥有相同德标签,或者说是有相同的类名,(当然id名不可能相 ...

  2. 纯小白入手 vue3.0 CLI - 1 - npm 安装与初始化

    node 开发环境请先自行准备 npm install -g @vue/cli 安装完成之后命令行则存在 vue 命令 vue -V 查看本地 vue 版本 vue -h 输出帮助 vue creat ...

  3. linux_kernel_uaf漏洞利用实战

    前言 好像是国赛的一道题.一个 linux 的内核题目.漏洞比较简单,可以作为入门. 题目链接: 在这里 正文 题目给了3个文件 分配是 根文件系统 , 内核镜像, 启动脚本.解压运行 boot.sh ...

  4. 多张报表导出到一个多sheet页excel

     业务需求: 通过勾选不同的报表名称,然后直接执行导出excel.并且这些报表需要统一导入到一个excel的多个sheet页中,并且对某些报表可能需要增加一些类似'已审核'之类的图片(展现时并没有 ...

  5. Android横、竖屏幕动态切换(layout-land 和layout-port)

    下面是一个例子程序: 1.首先通过以下语句设置Activity为无标题和全屏模式: // 设置为无标题栏 requestWindowFeature(Window.FEATURE_NO_TITLE); ...

  6. 带你从零学ReactNative开发跨平台App开发[react native SqlLite 终极运用](十二)

    ReactNative跨平台开发系列教程: 带你从零学ReactNative开发跨平台App开发(一) 带你从零学ReactNative开发跨平台App开发(二) 带你从零学ReactNative开发 ...

  7. Oracle EBS 新增OAFM个数

    在 $INST_TOP/ora/10.1.3/opmn/conf/opmn.xml中找到<process-type id="oafm" module-id="OC4 ...

  8. [SQLServer] 数据库SA用户被锁定或者忘记密码的恢复

    一.以管理员权限运行命令提示符 CMD C:\>net stop mssqlserver您想继续此操作吗? (Y/N) [N]: y C:\>net start mssqlserver / ...

  9. JS弹出div简单样式

    <div id="dialog" style="display:none;z-index:9999;position: absolute;border:1px so ...

  10. 转: c#.net利用RNGCryptoServiceProvider产生任意范围强随机数的办法

    //这样产生0 ~ 100的强随机数(含100) ; int rnd = int.MinValue; decimal _base = (decimal)long.MaxValue; ]; System ...