hdu6212 Zuma(区间dp)
#pragma GCC optimize(2) #include <bits/stdc++.h> #define ll long long #define ls(i) i<<1 #define rs(i) i<<1|1 using namespace std; const int N = 200+10; char s[N]; int dp[N][N],a[N],n; int dfs(int i,int j) { if(dp[i][j]>=0) return dp[i][j]; if(i==j) return dp[i][j]=3-a[i]; dp[i][j]=2*n; for(int k=i;k<j;k++) dp[i][j]=min(dp[i][j],dfs(i,k)+dfs(k+1,j)); if((j-i)%2==0) { dp[i][j]=min(dp[i][j],dfs(i+1,j-1)+(a[i]+a[j]==2)); if(a[i]+a[j]<4) { for(int k=i+2;k<j;k+=2) { if(a[k]==1) { dp[i][j]=min(dp[i][j],dfs(i+1,k-1)+dfs(k+1,j-1)); } } } } return dp[i][j]; } int main(){ int T; cin>>T; int kase=0; while(T--) { scanf("%s",s+1); n=strlen(s+1); int m=0; for(int i=1;i<=n;i++) { if(s[i]!=s[i-1]) a[++m]=1; else a[m]++; } memset(dp,-1,sizeof(dp)); printf("Case #%d: %d\n",++kase,dfs(1,m)); } return 0; }