Codeforces 1178E. Archaeology

Portal

Firstly least one solution, consider induction to prove

First Obviously $ n 3 $ <=

Consider $ n = 4 $ time, then since $ s [1]! = S [2], s [3]! = S [4] $, and $ s [i] \ in {a, b, c} $ obviously it means the pigeonhole principle $ s [1], s [2] $ equal to at least a $ s [3] $ or $ s [4] $

Then we extend to both sides from the middle, two each on the left and the right combination of the two, so that each location will have two four positions contributions palindrome character, then it must solvability

Note that if $ n \ mod 4 = 2 $ or $ n \ mod will eventually be left with several left can not make up two by two to the right when $ 4 = 3, then we palindrome length into an odd number (first select a middle as a palindrome center)

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
using namespace std;
typedef long long ll;
inline int read()
{
    int x=0,f=1; char ch=getchar();
    while(ch<'0'||ch>'9') { if(ch=='-') f=-1; ch=getchar(); }
    while(ch>='0'&&ch<='9') { x=(x<<1)+(x<<3)+(ch^48); ch=getchar(); }
    return x*f;
}
const int N=2e6+7;
int n;
char s[N];
vector <int> ans;
void work(int l,int r)
{
    for(; l>=2&&r<=n-1; l-=2,r+=2)
    {
        vector <int> cnt[3];
        cnt[s[l-1]-'a'].push_back(l-1);
        cnt[s[r+1]-'a'].push_back(r+1);
        cnt[s[l]-'a'].push_back(l);
        cnt[s[r]-'a'].push_back(r);
        for(int k=0;k<3;k++)
            if(cnt[k].size()>1)
            {
                ans.push_back(cnt[k][0]),
                ans.push_back(cnt[k][1]);
                break;
            }
    }
}
int main()
{
    scanf("%s",s+1); n=strlen(s+1);
    int mid=1+n>>1;
    if(n%4) { ans.push_back(mid); work(mid-1,mid+1); }
    else work(mid,mid+1);
    sort(ans.begin(),ans.end());
    for(auto p: ans) printf("%c",s[p]);
    puts(""); return 0;
}

 

Guess you like

Origin www.cnblogs.com/LLTYYC/p/11612640.html