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++]
  • 相关阅读:
    updatepanel中不能使用fileupload的弥补方法
    AJAXPro用法,关于JS同步和异步调用后台代码的学习
    How do I get data from a data table in javascript?
    记不住ASP.NET页面生命周期的苦恼
    浅谈ASP.NET中render方法
    解决AjaxPro2中core.ashx 407缺少对象的问题
    ServU 6.0出来了
    关于X Server/Client和RDP的畅想
    这个Blog好像没有分页功能嘛
    AOC的显示器太烂了
  • 原文地址:https://www.cnblogs.com/lzj-0218/p/3234138.html
Copyright © 2011-2022 走看看