题目链接:https://codeforces.com/contest/1178/problem/E
题意:给出一个只包含 的字符串
,保证任意两个连续的字符都不相同,要求选出一个子序列
使得它是一个回文串,并且
。
思路:贪心从两端取,因为任意两个连续的字符都不相同,再根据抽屉原理,左端的两个字符和右端的两个字符,必然会有两个相等,所以我们两个两个找即可。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ul;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define mp make_pair
#define pb push_back
#define all(x) x.begin(), x.end()
#define bug prllf("*********\n")
#define debug(x) cerr<<#x<<" = "<<(x)<<endl
#define debugp(x) cerr<<#x<<"= {"<<(x.first)<<", "<<(x.second)<<"}"<<endl
#define debug2(x, y) cerr<<"{"<<#x<<", "<<#y<<"} = {"<<(x)<<", "<<(y)<<"}"<<endl
#define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3fLL;
const int mod = 998244353;
const double eps = 1e-8;
const double pi = acos(-1);
const int N = 1e6 + 7;
string s;
int vis[N];
int main()
{
IO; cin >> s;
int l = 0, r = s.length() - 1;
while(l <= r)
{
if(l == r) {vis[l] = 1; break;}
if(s[l] == s[r]) vis[l] = vis[r] = 1;
else if(s[l] == s[r-1]) vis[l] = vis[r-1] = 1;
else if(s[l+1] == s[r]) vis[l+1] = vis[r] = 1;
else if(s[l+1] == s[r-1]) vis[l+1] = vis[r-1] = 1;
l += 2, r -= 2;
}
for(int i = 0; i < s.length(); i++)
if(vis[i]) cout << s[i];
cout << '\n';
}