蒜头君喜欢中心对称的字符串,即回文字符串。现在蒜头君手里有一个字符串 SS,蒜头君每次都会进行这样的操作:从 SS 中挑选一个回文的子序列,将其从字符串 SS 中去除,剩下的字符重组成新的字符串 SS。
蒜头君想知道,最少可以进行多少次操作,可以消除整个字符串。
输入格式
输入一行。输入一个字符串 SS(1 \leq length(S) \leq 161≤length(S)≤16),字符串均由小写字母组成。
输出格式
输出一行,输出一个整数,表示消除整个字符串需要的最少操作次数。
样例输入
abaccba
样例输出
2
解题说明:
ac代码:
#include<iostream>
#include<algorithm>
#include<string.h>
#include<string>
using namespace std;
const int inf=0x3f3f3f3f;
string st;
int judge(int x){
string st1="",st2;
int cot=0;
while(x){
if(x&1)st1+=st[cot];
x>>=1;
cot++;
}
st2=st1;
reverse(st1.begin(),st1.end());
if(st1==st2)return 1;
return 0;
}
int dp[70000];
int main(){
cin>>st;
int len=st.size();
for(int i=1;i<(1<<len);i++){
judge(i)?dp[i]=1:dp[i]=inf;
for(int j=i;j;j=(j-1)&i)
dp[i]=min(dp[i],dp[j]+dp[j^i]);
}
cout<<dp[(1<<len)-1]<<endl;
return 0;
}