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.
题解分析:
格式方面除了点问题,导致解决时间过长。。我用的方法是判断相邻两个字母是否相同,如果相同则记录,再继续判断,最后记录出的值就是取走石头的数
#include <stdio.h>
int main()
{
int n,i;
while (scanf("%d",&n)!=EOF)
{
int k=0;
char a[50];
scanf("%s",a);
for(i=0;a[i]!='\0';i++)
{if(a[i]==a[i+1])
k++;
}printf("%d",k);
printf("\n");
}return 0;
}