翻译来源:Ice and Fire - 洛谷
1.分析题目:
首先,根据题意得,最后一轮为1时最小的1一定没有获胜机会,同理,最后一轮为0时最大的4也没有获胜机会。
由小结构推大结构。
若字符串后缀为11时,与上次相比,只是让次小值2不能获胜,但又新添一个参赛玩家。因此,获胜的人数不变。
若字符后缀为00时,同上。
若字符后缀为10时,我们知道只要后缀为0,那么1一定能够获胜,于是,我们可以让1一路获胜,最后被任意除1以外的数字击败。
字符后缀为01时,也同上。
2.代码
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
string s;
cin >> s;
int len = s.length();
int sum = 1;
int cnt = 2;
cout << sum << ' ';
for (int i = 1; i < len; i++) {
cnt++;
if (s[i] == s[i - 1]) {
cout << sum << ' ';
} else {
sum = cnt - 1;
cout << sum << ' ';
}
}
cout << '\n';
}
return 0;
}