一开始少看了条件:相邻两个字符不相等,字符集只有abc
那么我们取开头结尾各两个字符,一定有两个是相等的
#include<bits/stdc++.h> using namespace std; const int maxn = 1000010; char s[maxn]; bool v[maxn]; int main() { scanf("%s", s + 1); int n = strlen(s + 1); int l, r, ans = 0; for (l = 1, r = n - 1; l + 2 <= r; l += 2, r -= 2) { for (int j = 0; j < 2; j++) for (int k = 0; k < 2; k++) { if(s[l + j] == s[r + k]) { v[l + j] = v[r + k] = 1; ans += 2; goto ed; } } ed: continue; } if(ans < n / 2) { v[l] = 1; } for (int i = 1; i <= n; i++) { if(v[i] == 1) printf("%c", s[i]); } printf(" "); }