我是靠谱客的博主 成就花生,最近开发中收集的这篇文章主要介绍CodeForces-266A Stones on the Table(语法练习题)Stones on the Table,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

Stones on the Table

time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
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
inputCopy
3
RRG
outputCopy
1
inputCopy
5
RRRRR
outputCopy
4
inputCopy
4
BRBG
outputCopy
0

问题简述:

  给出一个字符串,由"B",“R”,"G"组成,分别代表蓝色石头,红色石头,绿色石头,从中去掉几块石头才能使得相邻石头颜色不一样?

问题分析:

  对字符串做处理,将相邻的相同字符只保留一个,统计去掉的字符数。

程序说明:

  从头开始遍历字符串,若当前字符与下一个相同,去掉下一个,统计去掉的数量。

程序实现:

#include<iostream>
#include<string>
using namespace std;
int main()
{
int n,sum=0;
string str;
cin>>n>>str;
for(int i=0;i<str.size()-1;i++)
{
if(str[i]==str[i+1])
{
str.erase(i+1,1);
i--;
sum++;
}
}
cout<<sum;
}

最后

以上就是成就花生为你收集整理的CodeForces-266A Stones on the Table(语法练习题)Stones on the Table的全部内容,希望文章能够帮你解决CodeForces-266A Stones on the Table(语法练习题)Stones on the Table所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(53)

评论列表共有 0 条评论

立即
投稿
返回
顶部