题解:P14730 [ICPC 2022 Seoul R] Palindrome Type

· · 题解

题目大意

最后附上代码:

#include <iostream>
#include <unordered_set>
using namespace std;
typedef long long ll;
string s;
int n;
unordered_set<ll>bad;
bool can(int l,int r,int ans)
{
    while(l<r and s[l]==s[r]){l++;r--;}
    if(l>=r)return true;
    if(ans==0)return false;
    ll key=((ll)l<<40)|((ll)r<<2)|ans;
    if(bad.count(key))return false;
    if(can(l+1,r,ans-1))return true;
    if(can(l,r-1,ans-1))return true;
    bad.insert(key);
    return false;
}
int main()
{
    cin>>s;
    n=s.size();
    for (int k=0;k<=3;k++)
    {
        bad.clear();
        if(can(0,n-1,k))return cout<<k,0;
    }
    cout<<-1;
    return 0;
}