A. One-dimensional Japanese Crossword
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Recently Adaltik discovered japanese crosswords. Japanese crossword is a picture, represented as a table sized a × b squares, and each square is colored white or black. There are integers to the left of the rows and to the top of the columns, encrypting the corresponding row or column. The number of integers represents how many groups of black squares there are in corresponding row or column, and the integers themselves represents the number of consecutive black squares in corresponding group (you can find more detailed explanation in Wikipedia https://en.wikipedia.org/wiki/Japanese_crossword).

Adaltik decided that the general case of japanese crossword is too complicated and drew a row consisting of n squares (e.g. japanese crossword sized 1 × n), which he wants to encrypt in the same way as in japanese crossword.

The example of encrypting of a single row of japanese crossword.

Help Adaltik find the numbers encrypting the row he drew.

Input

The first line of the input contains a single integer n (1 ≤ n ≤ 100) — the length of the row. The second line of the input contains a single string consisting of n characters 'B' or 'W', ('B' corresponds to black square, 'W' — to white square in the row that Adaltik drew).

Output

The first line should contain a single integer k — the number of integers encrypting the row, e.g. the number of groups of black squares in the row.

The second line should contain k integers, encrypting the row, e.g. corresponding to sizes of groups of consecutive black squares in the order from left to right.

Examples
input
Copy
3
BBW
output
Copy
1
2 
input
Copy
5
BWBWB
output
Copy
3
1 1 1 
input
Copy
4
WWWW
output
Copy
0
input
Copy
4
BBBB
output
Copy
1
4 
input
Copy
13
WBBBBWWBWBBBW
output
Copy
3
4 1 3 

题意看的一脸懵逼,仔细看了看样例感觉应该是求整个字符串中连续的B的个数,每段输出,居然过了,有时候还要靠猜测题意了。。。

AC 代码:

#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
char c[10005];
int re[10005];
int main()
{
	int n;
	cin>>n;
	memset(re,0,sizeof(re));
	for(int i=0;i<n;i++)
	{
		cin>>c[i];
	}
	int k=0,sum=0;
	for(int i=0;i<n;i++)
	{
		int a=0;
		if(c[i]=='B')
		{
			while(c[i]=='B')
			{
				a++;//连续区间内B的个数
				i++;
			}
			sum++;//连续区间的个数
			re[k++]=a;
		}
	}
	cout<<sum<<endl;
	if(sum)
	{
		for(int i=0;i<k;i++)
		{
			if(i) cout<<" "<<re[i];
			else cout<<re[i];
		}
	cout<<endl;	
	}
 } 
推荐内容
阅读全文
AI总结
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐