#include <bits/stdc++.h>
using namespace std;
string s;
vector<vector<int> > dp;
int n;
int main() {
ios::sync_with_stdio(0);
cin.tie(NULL);
cin>>n;
cin>>ws;
cin>>s;
dp.assign(2,vector<int>(n,-1));
for(int i=n-1;i>=0;i--){
for(int j=0;j<n;j++){
if(i>=j)continue;
if(s[i]==s[j]){dp[0][j]=((i+1>=j-1)?0:dp[1][j-1]);continue;}
int l,r;
if(i+1>=j)l=0;
else l=dp[1][j];
if(i>=j-1)r=0;
else r=dp[0][j-1];
dp[0][j]=1+min(l,r);
}
dp[1]=dp[0];
dp[0]=vector<int>(n,-1);
}
cout<<dp[1][n-1];
}
Comments
Post a Comment