题目链接: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;
}

最新文章

  1. tensorflow学习笔记四:mnist实例--用简单的神经网络来训练和测试
  2. 使用XML文件记录操作日志,并从后往前读取操作日志并在richTextBox1控件中显示出来
  3. 【BZOJ 4517】【SDOI 2016 Round1 Day2 T2】排列计数
  4. 让IE支持placeholder属性~
  5. jacob 实现Office Word文件格式转换
  6. Nginx 开启 path_info功能
  7. Linq编程101例
  8. Linux控制台下的快捷键
  9. PHP接口和抽象类的区别
  10. CSS3 animation-timing-function steps()
  11. mysql数据库的基本操作:索引、视图,导入和导出,备份和恢复
  12. html中的Session
  13. 【XSY2519】神经元 prufer序列 DP
  14. zabbix监控Mysql中的QPS/TPS
  15. srs2.0安装问题
  16. 【示例】Spring Quartz入门
  17. php开启与关闭错误提示
  18. android DDMS中的内存监测工具Heap
  19. ios 开发之本地推送
  20. [Music] Billboard Hot 100 Singles Chart 27th Jun 2015

热门文章

  1. Network | sk_buff
  2. ubuntu系统克隆
  3. codeforces A. In Search of an Easy Problem
  4. kafka技术分享02--------kafka入门
  5. UITableView 滚动时使用reloaddata出现 crash&#39;-[__NSCFArray objectAtIndex:]: index (1) beyond bounds (0)&#39; Crash
  6. iOS -- SKTransition类
  7. iOS -- 原生NSStream实现socket
  8. EasyHook库系列使用教程之四钩子的启动与停止
  9. vim 树形菜单插件NERDTree 的安装
  10. php程序调试: xdebug的配置