C - Stones on the Table
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 ;
}
C - Stones on the Table的更多相关文章
- [LeetCode] Nim Game 尼姆游戏
You are playing the following Nim Game with your friend: There is a heap of stones on the table, eac ...
- [leetcode] 数字游戏
169. Majority Element Given an array of size n, find the majority element. The majority element is t ...
- LeetCode 292. Nim Game
Problem: You are playing the following Nim Game with your friend: There to stones. The one who remov ...
- 292. Nim Game
292. Nim Game You are playing the following Nim Game with your friend: There is a heap of stones on ...
- Nim Game,Reverse String,Sum of Two Integers
下面是今天写的几道题: 292. Nim Game You are playing the following Nim Game with your friend: There is a heap o ...
- Nam Game
哪一方最终给对方留下4的倍数个石头则win,即想方设法的给对方留下4的倍数个石头. 例如: 9(B win) A:3 B:2(B取2,给对方余4,对方则lose) A:1 | 2 | 3 B:3 | ...
- lleetcode 292. Nim Game
You are playing the following Nim Game with your friend: There is a heap of stones on the table, eac ...
- Codeforces Round #163 (Div. 2)
A. Stones on the Table \(dp(i)\)表示最后颜色为\(i\)的最长长度. B. Queue at the School 模拟. C. Below the Diagonal ...
- LN : leetcode 292 Nim Game
lc 292 Nim Game 292 Nim Game You are playing the following Nim Game with your friend: There is a hea ...
随机推荐
- 百度编辑器ueditor1.4.3配置记录
我从官网下载的php文件,但是图片上传不能用,后来查找资料,打开ueditor下的php/controller.php,(其他环境选对应的文件夹)把时区设置按如下改个字母大小写,再打开该文件就正确返回 ...
- Python OS & sys模块
os模块(* * * *) os模块是与操作系统交互的一个接口 os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径 os.chdir("dirname" ...
- js调用ro的webservice
Enabling JavaScript Access on the Server Drop the JavaScriptHttpDispatcher component onto the server ...
- 并发通信Manage,队列, 互斥锁
目录 Manage 队列 先入先出 互斥锁 Manage 进程间的通信是被限制的 from multiprocessing import Process a = 1 def func(): glob ...
- codeforeces近日题目小结
题目源自codeforeces的三场contest contest/1043+1055+1076 目前都是solved 6/7,都差了最后一题 简单题: contest/1043/E: 先不考虑m个限 ...
- crontab定时任务安装、使用方法
本文介绍下,在linux中安装crontab的方法,以及crontab的具体用法,有需要的朋友参考下. 这里使用yum方式安装crontab: 复制代码代码示例: [root@CentOS ~]# ...
- O - String Problem KMP 字符串最小表示法
Give you a string with length N, you can generate N strings by left shifts. For example let consider ...
- fork me on github 彩带设置无效
挑选彩带地址: https://github.com/blog/273-github-ribbons 发现代码复制粘贴过来,但是在自己博客园上无效,如粘贴如下代码 <a href="h ...
- Android:阻止输入法将图片压缩变形
Scrollview定义中添加一行: android:isScrollContainer="false"
- maven的启动类和MAVEN_OPTS参数
maven的启动类和MAVEN_OPTS参数 在mvn.cmd的155行, set CLASSWORLDS_LAUNCHER=org.codehaus.plexus.classworlds.launc ...