1423: 统计字符对
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 196 Solved: 134
Submit Status Web Board
Description
对于一个长度为n(0<n<1000)的字符串,请问在这个字符串中有多少对相邻的字符是相同的。字符串全部由英文小写字母构成。
Input
多组测试实例。
每个测试实例包含一个整数n(0<n<1000),表示字符串的长度。
接下来是一个长度为n的字符串。题目保证字符串全部由英文小写字符构成。
Output
输出一个整数,表示在字符串中相邻的相同字符的对数。
Sample Input
3aaa4aabb
Sample Output
22
HINT
Source
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
int n,count,len,i;
char a[1000];
while(scanf("%d",&n)!=EOF)
{
cin>>a;
len=strlen(a);
count=0;
for(i=0;i<len;i++)
{
while(i<len-1 && a[i]==a[i+1]) // aaa bb c dd
{
count++;
i++;
}
}
cout<<count<<endl;
}
return 0;
}