CodeForces - 266A Stones on the Table

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)

问题简述:桌子上n个(i<=n=50)有三种颜色的石头,记红色为"R",绿色为"G",蓝色为“B",需要计算要从桌子上取最少数量的石头。才能使桌子上剩下的石头相邻之间不会有颜色相同,桌子上的颜色及个数由自己输入,最后输出答案

问题分析;当n=3,石头的颜色为RRG时,输出的答案为1,即只需拿一个石头出来即可满足条件。当n=5,石头的颜色为RRRRR时,输出答案为4。当n=4,石头的颜色为BRBG时,输出的答案为0;可以发现,我们要拿出的石头个数为桌子上相同颜色石头个数减去一

程序分析:定义一个动态数组,长度为n(桌子上的石头的个数);定义一个函数find,函数中通过for语句来对桌子上的所有石头的颜色进行判断,如果相同,则局部变量count+1,最后通过函数调用把结果输出
AC的C++代码如下;

#include 
using namespace std;
int find(char*S,const int n)
{
    int count = 0;
    for (int i = 0; i <= n - 1; i++)
    {
        
        if (S[i] ==S[i + 1])
        {
            count++;
        }
    }
    return count;
}
int main()
{
    int n;
    
    while (cin >> n)
    {
        char* S = new char[n];
        cin >> S;
        cout<

你可能感兴趣的:(CodeForces - 266A Stones on the Table)