题目
思路
直接装压dp!?
看来是我想复杂了……
code:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<string>
using namespace std;
string x;
int o[100010],ans[30][30],p[100010];
int dp[1<<20];
int main()
{
cin>>x;
for (int i=0;i<x.size();i++) o[i]=x[i]-'a';
sort(o,o+x.size());
int tot=unique(o,o+x.size())-o;
for (int i=0;i<x.size();i++)
{
p[i]=lower_bound(o,o+tot,x[i]-'a')-o;
if (i!=0)
{
ans[p[i-1]][p[i]]++;
}
}
memset(dp,0x3f,sizeof(dp));
dp[0]=1;
for (int i=1;i<(1<<tot);i++)
{
for (int j=0;j<tot;j++)
{
if (i&1<<j)
{
int wj=dp[i^(1<<j)];
for (int k=0;k<tot;k++)
{
if (i&1<<k)
{
wj+=ans[j][k];
}
}
dp[i]=min(dp[i],wj);
}
}
}
cout<<dp[(1<<tot)-1];
return 0;
}