Codeforces721 A. One-dimensional Japanese Crossword

Codeforces721 A

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.

题目大意:

统计黑色方块个数并计算长度

关键代码,主要这一部分值得记录:

1.if(a[i] == 'B')  //B代表黑色方块,W代表白色方块
2.{  
3.      if(i == 0 || s[i-1] == 'W')  k++;           //k代表黑色方块个数
4.      a[k]++;                                    //a用来存每块黑色段的长度  
5.}  

本题完整代码:

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <map>
typedef long long LL;
using namespace std;

const int MaxN = 1e5 + 5; 
char s[105];
int a[105];

int main() {
    int n;
    scanf("%d %s", &n, s);
    int cnt = 0, k = 0;
    for(int i = 0; i < n; i++) {
        if(s[i] == 'B') {
            if(i == 0 || s[i - 1] == 'W') k++;
            a[k]++;
        }
    }
    printf("%d\n", k);
    for(int i = 1; i <= k; i++) {
        printf("%d", a[i]);
        if(i != k) printf(" ");
        if(i == k) printf("\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/jasmineaha/article/details/79190976