A. One-dimensional Japanese Crossword

本文介绍了一种基于日本填字游戏的加密方法,通过分析一串由黑白方格组成的行,将其转换为一组整数,用以表示连续黑色方格的组数及每组的长度。文章详细解释了算法实现过程,包括输入解析、状态跟踪和输出格式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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
inputCopy
3
BBW
outputCopy
1
2
inputCopy
5
BWBWB
outputCopy
3
1 1 1
inputCopy
4
WWWW
outputCopy
0
inputCopy
4
BBBB
outputCopy
1
4
inputCopy
13
WBBBBWWBWBBBW
outputCopy
3
4 1 3
Note
The last sample case correspond to the picture in the statement.

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
using namespace std;

int main()
{
    int n;
    while(cin >> n)
    {
        string s;
        cin >> s;
        int cnt = 0;
        int a[105];
        fill(a, a + sizeof(a) / sizeof(int), 1);
        bool flag = 0;
        for(int i = 0; s[i] != '\0'; ++i)
        {
            if(s[i] == 'B' && flag == 0 )
            {
                cnt++;
                flag = 1;
                continue;
            }
            if(s[i] == 'B' && flag )
                a[cnt]++;
            if(s[i] == 'W' )
                flag = 0;
        }
        cout << cnt << '\n';
        for(int i = 1; i <= cnt; ++i)
            cout << a[i] << ' ';
        cout << '\n';
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值