ACM 第三题

Stones on the Table
Time limit2000 ms
Memory limit262144 kB

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

问题链接:(https://vjudge.net/problem/CodeForces-266A)
问题简述:有多颗石头排成一列,把相邻同色的石头取出,要取出多少颗后使得相邻的石头不同色。
问题分析:先输入石头个数,用for循环和if语句判定有几颗石头相邻是同色的,最后输出结果。

AC通过的C语言程序如下:

#include<iostream>
using namespace std;
int main()
{
	int n = 0,b;
	cin >> b;
	char a[51];
	cin >> a;
	for (int i = 0; i < b-1; i++)
	{
		if (a[i] == a[i + 1])
		{
			n++;
		}
	}
	cout << n << endl;
}

猜你喜欢

转载自blog.csdn.net/weixin_43973938/article/details/84873985