版权声明:欢迎大家转载,转载请注明出处 https://blog.csdn.net/hao_zong_yin/article/details/82752809
对于两个位置i,j,他们的下一个位置分别为pi = (i*i+1)%n, pj = (j*j+1)%n, pi==pj的概率很高,因此只要把这种情况减掉就能过了
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;
const int maxn = 2e5 + 10;
struct Node {
int dep;
char c;
int p;
bool operator < (const Node &rhs) const {
if (dep != rhs.dep) return dep > rhs.dep;
if (c != rhs.c) return c < rhs.c;
return p > rhs.p;
}
};
char s[maxn];
char ans[maxn];
int main() {
int t, ks = 0;
scanf("%d", &t);
while (t--) {
int n;
scanf("%d", &n);
scanf("%s", s);
priority_queue<Node> que;
for (int i = 0; i < n; i++) que.push(Node{0, s[i], i});
char maxv = 0;
int predep = -1, prep = -1;
while (!que.empty()) {
Node node = que.top(); que.pop();
if (node.dep >= n) break;
if (node.dep != predep) {
maxv = node.c;
}
else maxv = max(maxv, node.c);
if (node.c < maxv) continue;
if (node.dep == predep && node.p == prep) continue;
predep = node.dep, prep = node.p;
ans[node.dep] = node.c;
int nex = (1LL*node.p*node.p+1)%n;
que.push(Node{node.dep+1, s[nex], nex});
}
printf("Case #%d: ", ++ks);
for (int i = 0; i < n; i++) printf("%c", ans[i]);
puts("");
}
return 0;
}