C - Stones on the Table
2024-08-31 03:43:46
Problem description
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
Input
5
RRRRR
Output
4
Input
4
BRBG
Output
0
解题思路:题目的意思就是输入的字符串中如果有相同的字符,去掉相同的字符,保留其中一个就好,保证序列中不出现相同的字符,问一共去掉多少个字符。简单处理字符串。
AC代码:
#include<bits/stdc++.h>
using namespace std;
int main(){
int n,i=,sum=,num=;char str[];
cin>>n;getchar();//吃掉回车符对字符串的影响
cin>>str;
while(str[i]!='\0'){
if(str[i+]==str[i]){
while(str[i+]==str[i]){num++;i++;}
}
else {sum+=num;num=;i++;}
}
cout<<sum<<endl;
return ;
}
最新文章
- Ajax深入解析
- arcgis arcengine Using environment settings
- 【解决】若要使用报表生成器,必须在此计算机上安装 .Net Framework 3.5
- Altium Designer15 卡在登陆界面解决办法:
- AngularJS:如何使用自定义指令来取代ng-repeat
- 高性能JavaScript DOM编程
- 设置php下载文件的超时时间
- XPATH基础入门资料
- Java- 类型转换
- 采用sharedPreference保存数据
- Objective-C基调(4)Category
- Spring3+SpingMVC+Hibernate4全注解环境配置
- 多线程中使用CheckForIllegalCrossThreadCalls = false访问窗口
- f5电源模块损坏
- flask实现简单的接收json返回json的接口
- Spring Boot的快速创建
- java基础学习:JavaWeb之Cookie和Session
- r语言 技巧总结
- Spring Cloud 入门 之 Eureka 篇(一)
- mockito測試框架