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 <iostream>
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;
}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 12,179评论 0 10
  • 感谢笑来老师在专栏强调了投资的重要性,我从一年前开始学习投资区块链,目前资产已经翻了几十倍。但94风暴后,国家禁止...
    周培仪阅读 5,105评论 0 6
  • 果然陷得太深~
    零九年白T恤阅读 1,490评论 0 0

友情链接更多精彩内容