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-权威指南学习笔记10

    第十章 正则表达式的模式匹配 1.正则表达式是一个描述字符模式的对象. 2.可以使用RegExp()构造函数来创建RegExp对象,不过RegExp对象更多是通过一种特殊的直接量语法来创建. 3.程序 ...

  2. 前端HTML空格与后台PHP utf-8空格

    今天在处理html input输入框时,发现一个问题: 在用户名输入框中输入admin "'p(中间是一个空格),点保存后台提示数据保存成功,按理应该是未修改,通过chrome调试工具发现传 ...

  3. 解决MyEclipse不能导出war包

    原因:无法导出是由于软件破解不完成导致的: 解决办法: 找到MyEclipse安装目录下MyEclipse\Common\plugins文件夹中的com.genuitec.eclipse.export ...

  4. springMVC结合AjaxForm上传文件

    最近在项目中需要上传文件文件,之前一直都是form提交的,尝试了一下AjaxForm,感觉还比较好用,写篇随笔mark下,供以后使用. 准备工作: 下载jquery-form.js 相关jar: co ...

  5. webpack+express多页站点开发

    学习了webpack门级的教程后,觉得可能是专门为单页应用而量身打造的,比如webpack+react.webpack+vue等,都可以解决各种资源的依赖加载.打包的问题.甚至css都是打包在js里去 ...

  6. 多线程应用-类(thread)

    在对class thread加锁时,锁无法正常应用,函数方式没问题. 在使用class thread方法时,并发后的查询结果不对,函数方式没问题. # -*- coding: UTF-8 -*- fr ...

  7. Linux下安装Tomcat7

    一.Tomcat7软件包下载 Tomcat下载地址http://tomcat.apache.org/download-70.cgi 下载完成后, 将软件包apache-tomcat-7.0.82.ta ...

  8. Linux chown命令详解

    chown将指定文件的拥有者改为指定的用户或组,用户可以是用户名或者用户ID:组可以是组名或者组ID:文件是以空格分开的要改变权限的文件列表,支持通配符. chown常见命令参数 Usage: cho ...

  9. RedHat 7 安装PostgreSQL 10.5

    系统环境 Redhat: Version: 7.4.1708 Architecture: x86_64 Address: 10.127.1.11 User: root Uassword: redhat ...

  10. Hadoop HBase概念学习系列之HBase里的Client(二十二)

    这个,很简单,但凡是略懂大数据的,就很清楚,不多说,直接上图.