概述
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所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复