4654. 消除游戏 - AcWing题库
#include <iostream>
#include <vector>
#include <cstring>
using namespace std;const int N = 1e6 + 10;
char s[N];
int l[N], r[N];
vector<int> v, p;
bool st[N];
void ins(int i) {if (st[i]) return;st[i] = true;p.push_back(i);return;
}
void fun() {p.clear();for (int i : v) {int a = l[i], b = i, c = r[i];if (s[a] == s[b] && s[b] != s[c] && s[c] != '_') {ins(b), ins(c);}else if (s[a] != s[b] && s[b] == s[c] && s[a] != '_') {ins(a), ins(b);}}return;
}
int main() {cin >> s + 1;int n = strlen(s + 1);s[0] = s[n + 1] = '_';for (int i = 1; i <= n; i++) {l[i] = i - 1, r[i] = i + 1;v.push_back(i);}r[0] = 1, l[n + 1] = n;while (true) {fun();if (p.empty()) break;v.clear();for (int i : p) {int a = l[i], b = i, c = r[i];if (!st[a] && a && (v.empty() || v.back() != a)) v.push_back(a);if (!st[c] && c != n + 1) v.push_back(c);r[a] = c, l[c] = a;}}if (r[0] == n + 1) puts("EMPTY");else {for (int i = r[0]; i != n + 1; i = r[i]) {cout << s[i];}}return 0;
}