zoukankan      html  css  js  c++  java
  • Codeforces 266A Stones on the Table

    Stones on the Table
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard 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.

    Sample test(s)
    input
    3 RRG
    output
    1
    input
    5 RRRRR
    output
    4
    input
    4 BRBG
    output
    0


    今天终于能上去Codeforces了,激动好久(之前还以为遭到方校长封杀,校园网根本上不去呢……

    于是开始在DP里找水题做,可这过得人数最多的一道题,真没看出来跟DP有啥关系……

    大水题就是把相邻且相同的字母删到只剩一个,直接上代码吧……

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 
     5 using namespace std;
     6 
     7 int len;
     8 char s[100];
     9 
    10 int main()
    11 {
    12     while(scanf("%d",&len)==1)
    13     {
    14         int ans=0;
    15         getchar();
    16         gets(s);
    17         for(int i=0;i<len;i++)
    18         {
    19             int j;
    20             for(j=i+1;j<len&&s[i]==s[j];j++)
    21                 ans++;
    22             i=j-1;
    23         }
    24         printf("%d
    ",ans);
    25     }
    26 
    27     return 0;
    28 }
    [C++]
  • 相关阅读:
    apache log4j打印日志源码出口
    filter listener interceptor的区别
    搭建oracle linux虚拟机报错解决
    top命令查看进程下线程信息以及jstack的使用
    关于跨域访问
    跨域访问
    一直性hash解决扩容后的hash算法不用变
    _创建日志_
    oracle读写文件--利用utl_file包对磁盘文件的读写操作
    oracle中utl_file包读写文件操作实例学习
  • 原文地址:https://www.cnblogs.com/lzj-0218/p/3234138.html
Copyright © 2011-2022 走看看