#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
int maxn = 0x3f3f3f3f;
string S;
bool isPalindrome(int t)
{
string a, b;
int id = 0;
while(t > 0)
{
if(t & 1)
{
a += S[id];
}
t >>= 1;
id++;
}
b = a;
reverse(b.begin(), b.end());
if(a == b)
return true;
else
return false;
}
int main()
{
cin >> S;
int len = S.length();
int dp[(1 << len) + 1];
memset(dp, maxn, sizeof(dp));
for (int t = 0; t < (1 << len); t++)// 枚举当前状态
{
if(isPalindrome(t))// 判断当前状态是否是回文,如果是回文则步骤数为 1
{
dp[t] = 1;
continue;
}
for(int i = t; i; i = (i - 1) & t)// 枚举 t 的所有子集
{
dp[t] = min(dp[t], dp[i] + dp[t ^ i]); // 更新当前状态的解的最小值
}
}
cout << dp[(1 << len) - 1] << endl;
return 0;
}
计蒜客 消除字符串(DP)
猜你喜欢
转载自blog.csdn.net/ccshijtgc/article/details/80906793
今日推荐
周排行