题目
有一根笔,初始为红色,写一个字就会在红和蓝之间切换一次。现在有一个字符串由 和 组成,你需要从左往右写,写 时必须为红色,写 时必须为蓝色,可以手动切换颜色,问写完这个字符串最少手动切换几次颜色。字符串长度 ,测试点数 。
思路
根据题意,只要前后颜色相同就要手动切换,注意如果开头不是 就还要手动切换一次。
代码
#include<bits/stdc++.h>
using namespace std;
#define il inline
#define rg register
#define inf 0x3f3f3f3f
int a,b,d;
string c;
int main()
{
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
cin>>a;
while(a--)
{
cin>>b>>c;
d=0;
if(c[0]!='R')
{
d++;
}
for(rg int i=1;i<b;i++)
{
if(c[i]==c[i-1])
{
d++;
}
}
cout<<d<<'\n';
}
return 0;
}
/*
*/