题目链接:http://codeforces.com/contest/721/problem/A

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
3
BBW
output
1
2
input
5
BWBWB
output
3
1 1 1
input
4
WWWW
output
0
input
4
BBBB
output
1
4
input
13
WBBBBWWBWBBBW
output
3
4 1 3
Note

The last sample case correspond to the picture in the statement.

题解:

虽然是一道很基础的题目,但是却清晰了我对类似情况的写法, 值得记录一下。

关键代码:

if(a[i]=='B')
{
if(i==0 || a[i-1]=='W') k++;
s[k]++;
}

代码如下:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <sstream>
#include <algorithm>
using namespace std;
#define ms(a, b) memset((a), (b), sizeof(a))
#define eps 0.0000001
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+7;
const int maxn = 100+10; int n;
char a[maxn];
int k, s[maxn]; void init()
{
scanf("%d",&n);
scanf("%s",a);
ms(s,0);
k = 0;
} void solve()
{
int cnt = 1;
for(int i = 0; i<n; i++)
{
if(a[i]=='B')
{
if(i==0 || a[i-1]=='W') k++;
s[k]++;
}
} printf("%d\n",k);
for(int i = 1; i<=k; i++)
printf("%d ",s[i]);
putchar('\n');
} int main()
{
// int T;
// scanf("%d",&T);
// while(T--)
{
init();
solve();
}
return 0;
}

Codeforces Round #374 (Div. 2) A. One-dimensional Japanese Crossword —— 基础题的更多相关文章

  1. Codeforces Round #368 (Div. 2) A. Brain's Photos (水题)

    Brain's Photos 题目链接: http://codeforces.com/contest/707/problem/A Description Small, but very brave, ...

  2. Codeforces Round #374 (Div. 2) A. One-dimensional Japanese Crosswor 水题

    A. One-dimensional Japanese Crossword 题目连接: http://codeforces.com/contest/721/problem/A Description ...

  3. 拓扑序+dp Codeforces Round #374 (Div. 2) C

    http://codeforces.com/contest/721/problem/C 题目大意:给你有向路,每条路都有一个权值t,你从1走到n,最多花费不能超过T,问在T时间内最多能访问多少城市? ...

  4. Codeforces Round #374 (Div. 2) D. Maxim and Array 贪心

    D. Maxim and Array 题目连接: http://codeforces.com/contest/721/problem/D Description Recently Maxim has ...

  5. Codeforces Round #374 (Div. 2) C. Journey DP

    C. Journey 题目连接: http://codeforces.com/contest/721/problem/C Description Recently Irina arrived to o ...

  6. Codeforces Round #374 (Div. 2) B. Passwords 贪心

    B. Passwords 题目连接: http://codeforces.com/contest/721/problem/B Description Vanya is managed to enter ...

  7. Codeforces Round #374 (Div. 2) D. Maxim and Array —— 贪心

    题目链接:http://codeforces.com/problemset/problem/721/D D. Maxim and Array time limit per test 2 seconds ...

  8. Codeforces Round #374 (Div. 2) C. Journey —— DP

    题目链接:http://codeforces.com/contest/721/problem/C C. Journey time limit per test 3 seconds memory lim ...

  9. Codeforces Round #374 (Div. 2) B. Passwords —— 基础题

    题目链接:http://codeforces.com/contest/721/problem/B B. Passwords time limit per test 2 seconds memory l ...

随机推荐

  1. CDOJ_149 解救小Q

    原题网址:http://acm.uestc.edu.cn/#/problem/show/149 小Q被邪恶的大魔王困在了迷宫里,love8909决定去解救她. 迷宫里面有一些陷阱,一旦走到陷阱里,就会 ...

  2. 51NOD 1833 环

    考虑一下简单环覆盖这个图的意义,其实就是找出原序列的所有排列,满足所有<i,a[i]>都是原图中的一条有向边. 因为一个置换就是由很多简单环构成的. 于是我们可以设 f[i][S] 为考虑 ...

  3. C# MD5加密(16进制)

    MD5加密(16进制) vs会提示引用 using System.Security.Cryptography; 代码如下: public static string MD5Encrypt32(stri ...

  4. Java -----transient 和static

    越来越喜欢深究java基础了,讲讲 transient  和static 对序列化的影响.废话少说,直接上代码就可以了 package serializable; import java.io.Ser ...

  5. TF-IDF学习笔记

    计算文本的权重向量,有个很有效的权重方案:TF-IDF权重策略.TF-IDF含义是词频逆文档频率,指的是,如果某个词或短语在一篇文章中出现的频率高,并且在其他文章中很少出现,则认为此词或短语具有很好的 ...

  6. Java中判断String对象是否为空的方法

    Java原生的方法: String对象中有一个isEmpty的方法判断是否为空,其实isEmpty完全等同于string.length()==0,注意如果String本身是null,那么使用strin ...

  7. Maven自动部署war到Tomcat8

    原文:http://www.cnblogs.com/yucongblog/p/5392932.html 我使用的环境是:Eclipse Java EE IDE for Web Developers(V ...

  8. lstm公式推导

    http://blog.csdn.net/u010754290/article/details/47167979 导言 在Alex Graves的这篇论文<Supervised Sequence ...

  9. linux内核I2C子系统学习(三)

    写设备驱动: 四部曲: 构建i2c_driver 注册i2c_driver 构建i2c_client ( 第一种方法:注册字符设备驱动.第二种方法:通过板文件的i2c_board_info填充,然后注 ...

  10. x264 编码数配置

    记录项目中用到一组x264快速编码参数配置,具体如下: param->i_frame_reference = 1; param->i_scenecut_threshold = 0; par ...