这是蓝桥杯经典题“拉马车”,直接模拟即可
2026-09-13 14:10:49
发布于:广东
13阅读
0回复
0点赞
#include <bits/stdc++.h>
using namespace std;
static char qA[10005], qB[10005];
int main() {
char buf[64];
scanf("%s", buf);
int hA = 0, tA = 0;
for (int i = 0; buf[i]; i++) qA[tA++] = buf[i];
scanf("%s", buf);
int hB = 0, tB = 0;
for (int i = 0; buf[i]; i++) qB[tB++] = buf[i];
static char table[10005];
int top = 0;
int cnt[128] = {0};
int turn = 0;
int steps = 0;
while (hA < tA && hB < tB) {
if (++steps > 10000) { puts("-1"); return 0; }
char c;
if (turn == 0) c = qA[hA++];
else c = qB[hB++];
if (cnt[(int)c] > 0) {
if (turn == 0) {
qA[tA++] = c;
while (true) {
char t = table[--top];
cnt[(int)t]--;
qA[tA++] = t;
if (t == c) break;
}
} else {
qB[tB++] = c;
while (true) {
char t = table[--top];
cnt[(int)t]--;
qB[tB++] = t;
if (t == c) break;
}
}
} else {
table[top++] = c;
cnt[(int)c]++;
turn ^= 1;
}
}
if (hA >= tA) {
for (int i = hB; i < tB; i++) putchar(qB[i]);
} else {
for (int i = hA; i < tA; i++) putchar(qA[i]);
}
putchar('\n');
return 0;
}
全部评论 1

2026-09-13 来自 广东
0





有帮助,赞一个