Codeforces Round #824 (Div. 2) C、D题题解

本文提供了Codeforces竞赛中C题“PhaseShift”与D题“Meta-set”的解题思路及代码实现。针对C题,采用并查集处理字符间的前后关系;D题则通过考虑末尾元素进行分类讨论。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Problem - C - Codeforces

C. Phase Shift

#include <bits/stdc++.h>
using namespace std;

const int N = 110;

int n, pre[N], nxt[N], fa[N];
string s;

int find(int u) {
  return u == fa[u] ? u : fa[u] = find(fa[u]);
}

int main()
 {
  ios::sync_with_stdio(0), cin.tie(0);
  int T; cin >> T;
  while (T--) {
    cin >> n >> s;
    for (int i = 0; i < 26; ++i) {
      pre[i] = nxt[i] = -1, fa[i] = i;
    }
    int cnt = 26;
    for (int i = 0; i < n; ++i) {
      int c = s[i] - 'a';
      if (~pre[c]) cout << char(pre[c] + 'a');
      else {
        for (int j = 0; j < 26; ++j) {
          if (j == c || ~nxt[j]) continue;
          if (cnt != 1 && find(c) == j) continue;
          nxt[j] = c, pre[c] = j;
          fa[j] = c, --cnt;
          break;
        }
        cout << char(pre[c] + 'a');
      }
    }
    cout << "\n";
  }
  return 0;
}

Problem - D - Codeforces

D. Meta-set

思路:是考虑末尾的那个元素,然后分类讨论了一车

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值