一、题目描述:
牛牛喜欢彩色的东西,尤其是彩色的瓷砖。牛牛的房间内铺有L块正方形瓷砖。每块砖的颜色有四种可能:红、绿、蓝、黄。给定一个字符串S, 如果S的第i个字符是'R', 'G', 'B'或'Y',那么第i块瓷砖的颜色就分别是红、绿、蓝或者黄。
牛牛决定换掉一些瓷砖的颜色,使得相邻两块瓷砖的颜色均不相同。请帮牛牛计算他最少需要换掉的瓷砖数量。
输入描述:
输入包括一行,一个字符串S,字符串长度length(1 ≤ length ≤ 10),字符串中每个字符串都是'R', 'G', 'B'或者'Y'。
输出描述:
输出一个整数,表示牛牛最少需要换掉的瓷砖数量
输入例子1:
RRRRRR
输出例子1:
3
二、C++代码实现:
1 #include<iostream> 2 #include<string> 3 using namespace std; 4 5 int main(){ 6 string str; 7 while(cin>>str){ 8 int ret = 0; 9 int n = str.size(); 10 int last = str[0]; 11 for(int i = 1 ; i < n ; ++i){ 12 if(str[i] == last){ 13 if(str[i] != 'R' && (i+1 < n && str[i+1] != 'R')) 14 str[i] = 'R'; 15 else if(str[i] != 'G' && (i+1 < n && str[i+1] != 'G')) 16 str[i] = 'G'; 17 else if(str[i] != 'B' && (i+1 < n && str[i+1] != 'B')) 18 str[i] = 'B'; 19 else 20 str[i] = 'Y'; 21 ++ret; 22 } 23 last = str[i]; 24 } 25 cout<<ret; 26 } 27 }