蒜頭君喜歡中心對稱的字符串,即迴文字符串。如今蒜頭君手裏有一個字符串 SS,蒜頭君每次都會進行這樣的操做:從 SS 中挑選一個迴文的子序列,將其從字符串 SS 中去除,剩下的字符重組成新的字符串 SS。html
蒜頭君想知道,最少能夠進行多少次操做,能夠消除整個字符串。ios
輸入一行。輸入一個字符串 SS(1 \leq length(S) \leq 161≤length(S)≤16),字符串均由小寫字母組成。spa
輸出一行,輸出一個整數,表示消除整個字符串須要的最少操做次數。code
abaccba
2
ac代碼:htm
#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; }