Codeforces Round #374 (Div. 2) A. One-dimensional Japanese Crossword —— 基础题
题目链接:http://codeforces.com/contest/721/problem/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.
Help Adaltik find the numbers encrypting the row he drew.
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).
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.
3 BBW
1 2
5 BWBWB
3 1 1 1
4 WWWW
0
4 BBBB
1 4
13 WBBBBWWBWBBBW
3 4 1 3
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; }
posted on 2017-05-13 14:05 h_z_cong 阅读(40420) 评论(0) 编辑 收藏 举报