CF1927B 题解
思路
这就是一个贪心题,若 ,则把 设为一个目前没有出现过的字符;否则就把 设为任意一个在此之前已经出现了 次的字符。但是数据范围太大,所以从 开始倒着枚举当然是不行的,因此我们可以使用 个队列, 里存的是所有在此之前已经出现了 次的字符。每次添加元素的时候(即当 时),我们就可以取出 的队头充当 ,并把它弹了再加到 里。
代码
# include <bits/stdc++.h>
using namespace std;
int t, n, x;
queue <char> q[200005];
char tot, ans;
int main () {
ios::sync_with_stdio (0);
cin.tie (0);
cout.tie (0);
cin >> t;
while (t --) {
cin >> n;
tot = 'a'; //tot 是指当前第一个没出现过的字符
for (int i = 0; i < n; ++ i) {
while (! q[i].empty ()) //多组数据要清空
q[i].pop ();
cin >> x;
if (x)
cout << (ans = q[x - 1].front ()), q[x - 1].pop (), q[x].push (ans); //在 q[x-1] 里找元素
else
cout << (ans = tot), q[0].push (tot), ++ tot;
}
cout << '\n';
}
return 0;
}

浙公网安备 33010602011771号