题解 CF721A 【One-dimensional Japanese Crossword】
发现我的评测是最快的!465ms
题目看得迷迷糊糊,看了样例就懂了,原本是想把答案存到数组,突然想起了 stringstream,直接把答案放在那里最后输出呗!
然后我又想,第一个读入不是没有用吗?为啥不用 scanf(%*d); 把它吞了呢?
但是碰到这种题目有一点要考虑!如果一直是 B,那循环完根本没有统计过,所以我们在读入的字符串后面加一个 W,这样就可以在最后再统计一遍啦!
代码:
#include <iostream>
#include <sstream>
#include <cstdio>
#include <string>
using namespace std;
int main()
{
stringstream ans;
string s;
scanf("%*d");
cin >> s;
s.push_back('W'); // 最好用自带函数,不然可能会RE
int len = s.length() - 1, cnt = 0, now_black = 0;
for (register int i = 0; i <= len; i++)
{
if (s[i] == 'B') now_black++;
else
{
if (now_black == 0) continue;
cnt++;
ans << now_black;
ans << " ";
now_black = 0;
}
}
ans << '\n';
cout << cnt << endl << ans.str();
system("pause");
return 0;
}

浙公网安备 33010602011771号