VJ第三题 Stones on the Table

VJ第三题 Stones on the Table
CodeForces - 266A

问题:
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.

Input

3
RRG

Output
1

Input

5
RRRRR

Output

4

Input

4
BRBG

Output

0

#include "stdafx.h"
#include<string>
#include<iostream>
using namespace std;

int main()
{
	int l; //l输入有几个石头
	string a[1]; //用来输入多少颜色 字符串类型
	cin >> l;
	cin >> a[0];
	int i=0,e,k=0; //i是前一位,k用来记录删除多少个石头,e是记录字符长度
	e = a[0].size();
	if (l==1)
	{
		printf("0");
	}
	else
	{
		for (; i+1 < e; i++)
		{
			if (a[0][i] == a[0][i+1])
			{
				k++;
				for (int j = i+1; j+1 < e; j++)
				{
					if (a[0][j] == a[0][j + 1])
					{
						k++;
						i = j;
						continue;
					}
					else
					{
						i = j;
						break;
					}
				}
			}
		}
		cout << k;
	}
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_26558047/article/details/84933591
今日推荐