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++]
原文地址:https://www.cnblogs.com/lzj-0218/p/3234138.html