zoukankan      html  css  js  c++  java
  • C

    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代码:
     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 int main(){
     4     int n,i=0,sum=0,num=0;char str[55];
     5     cin>>n;getchar();//吃掉回车符对字符串的影响
     6     cin>>str;
     7     while(str[i]!=''){
     8         if(str[i+1]==str[i]){
     9             while(str[i+1]==str[i]){num++;i++;}
    10         }
    11         else {sum+=num;num=0;i++;}
    12     }
    13     cout<<sum<<endl;
    14     return 0;
    15 }
    
    
  • 相关阅读:
    在linux上使用Android systrace
    perf性能调优
    未初始化内存检测(MSan)
    数据竞争检查工具(TSan)
    应用层内存溢出/越界/重复释放等问题检查工具(ASan)
    gperf heap profiler
    cmake打印shell
    github clone加速
    获取一个进程的所有物理地址上的内存
    Jenkins <1>: System Management
  • 原文地址:https://www.cnblogs.com/acgoto/p/9112415.html
Copyright © 2011-2022 走看看