ZOJ

题意:

一条长度为n的直线,你一开始在位置m上

其中每个整点都有一个字符'L'或'R',如果是'L'那么你必须往左走一步,否则往右走一步

如果你到达位置1或位置n你任务就完成了

不过有可能你永远到不了1或n,比如RRRRLLLL这样的情况

但你可以修改字符,求能完成任务的最小修改次数

思路:

如果往右走,碰到左转就变成右转,计算L的个数即可,反之也是

左右端点不计入结果

代码:

#include<iostream>
#include<cstdio>
using namespace std;
const int maxn = 1e5+10;
char str[maxn];
int main() {
	int t, a, b;
	scanf("%d", &t);
	while(t--) {
		int sum1 = 0, sum2 = 0;
		scanf("%d %d %s", &a, &b, str+1);
		for(int i = 2; i <= b; i++) 
			if(str[i] == 'R') sum1++;
		for(int i = b; i <= a-1; i++) {
			if(str[i] == 'L') sum2++;
		} 
		printf("%d
", sum1>sum2?sum2:sum1);
	} 	
	return 0;
}

原文地址:https://www.cnblogs.com/somliy/p/9737492.html