3

There are n stones on the table in a row, each of them can be red, green or blue. Count the minimum number of stones to take from the table so that any two neighboring stones had different colors. Stones in a row are considered neighboring if there are no other stones between them.
Input
The first line contains integer n (1 ≤ n ≤ 50) — the number of stones on the table.
The next line contains string s, which represents the colors of the stones. We'll consider the stones in the row numbered from 1 to n from left to right. Then the i-th character s equals "R", if the i-th stone is red, "G", if it's green and "B", if it's blue.
Output
Print a single integer — the answer to the problem.
Examples
Input
3
RRG
Output
1
Input
5
RRRRR
Output
4
Input
4
BRBG
Output
0
问题链接:https://vjudge.net/problem/CodeForces-266A
问题简述:有m个石头排成一排,他们有红蓝绿三种颜色,‘G’表示绿,‘R’表示红,‘B’表示蓝,找出最小数字n,使在这排石头中取出n个石头后每个石头相邻的石头颜色不一样。
问题分析:用整型存储石头数量m,用m来建立char动态数组,用num存储取出最小数量的石头,当当前石头不为空时,用if语句判断相邻石头是否颜色相同,如果相同则使num+1。可求出num。
AC通过的C++语言程序如下:

#include 
using namespace std;
int main()
{
    int sl;
    cin >> sl;
    char *p = new char[sl];
    cin >> p;
    int num = 0;
    for (int n = 0; n < sl; n++)
    {
        for (int k = n + 1; k < sl; k++)
        {
            if (p[n] == p[k])
            {
                num++;
                n++;
            }
            else break;
        }
    }
    cout << num;
    return 0;
}

你可能感兴趣的:(3)