【AtCoder】Beginner Contest 380-B.Hurdle Parsing

题目链接

Problem Statement

Iroha has a sequence of positive integers A = ( A 1 , A 2 , … , A N ) A = (A_1, A_2, \dots, A_N) A=(A1,A2,,AN) of length N N N ( N ≥ 1 N \ge 1 N1).
She generated a string S S S using A A A as follows:

  • Start with S = S = S= |.
  • For i = 1 , 2 , … , N i = 1, 2, \dots, N i=1,2,,N, perform the following operations in order:
    • Append A i A_i Ai copies of - to the end of S S S.
    • Then, append one | to the end of S S S.

Given the generated string S S S, reconstruct the sequence A A A.

中文题意

问题陈述

Iroha有一个长度为 N N N N ≥ 1 N \ge 1 N1 )的正整数序列 A = ( A 1 , A 2 , … , A N ) A = (A_1, A_2, \dots, A_N) A=(A1,A2,,AN)
她使用 A A A 生成了一个字符串 S S S ,如下所示:

  • S = S = S= ‘ | ’开头。
  • 对于 i = 1 , 2 , … , N i = 1, 2, \dots, N i=1,2,,N ,依次执行如下操作:
  • S S S 的末尾追加 A i A_i Ai 个“-”副本。
  • 然后,在 S S S 后面添加一个‘ | ’。

给定生成的字符串 S S S ,重建序列 A A A

Constraints

  • S S S is a string of length between 3 3 3 and 100 100 100, inclusive, generated by the method in the problem statement.
  • A A A is a sequence of positive integers of length at least 1 1 1.

Input

The input is given from Standard Input in the following format:

S

Output

Print the answer in the following format, with elements separated by spaces in a single line:

A 1 A_1 A1 A 2 A_2 A2 … \dots A N A_N AN

Sample Input 1

|---|-|----|-|-----|

Sample Output 1

3 1 4 1 5

$S = $ |---|-|----|-|-----| is generated by A = ( 3 , 1 , 4 , 1 , 5 ) A = (3, 1, 4, 1, 5) A=(3,1,4,1,5).

Sample Input 2

|----------|

Sample Output 2

10

Sample Input 3

|-|-|-|------|

Sample Output 3

1 1 1 6

解法

这里使用一个技巧,避开第一个 | ,直接从第二个字符开始计数,意味着把每一个---|看成一对。使用一个变量 cnt 统计 -的个数,如果遇到| 输出cnt,同时令cnt = 0 即可。

string s;
void solved() {
	/* your code */
	cin >> s;
	int cnt = 0;
	for (int i = 1; i < s.size(); i ++) {
		if(s[i] == '|') {
			cout << cnt << " ";
			cnt = 0;
		}
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值