problem-266A-Stones on the Table-codeforces

problem-266A-Stones on the Table-codeforces,第1张

题目:

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.

输入:

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.

输出:

Print a single integer — the answer to the problem.

Examples input

3

RRG

output

1

代码:
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
const int N = 2e5 + 10;
typedef long long ll;
typedef pair PII;



int main() {
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	int n;
	string s;
	cin >> n;
	cin >> s;
	int ans = 0;
	int i = 0;
	while (i < n) {
		int j = i;
		while (s[j] == s[j + 1])
			j++;
		ans += j - i;
		i = j + 1;
	}
	cout << ans << endl;
	return 0;
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/722128.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-26
下一篇 2022-04-26

发表评论

登录后才能评论

评论列表(0条)

保存