HDU 6212 Interval DP

https://vjudge.net/problem/HDU-6212

和BZOJ上的一道题挺像的

但是为什么我HDU上过了

BZOJ上没过??(大雾)

这题是很有象征意义的

因为是我大学入ACM坑里的一个标志

九个月前

学长他们在打青岛网络赛

当时我第一次见到这题

卧槽!这么难

不过现在我会写了

虽然自己只想出了一半吧

还是那句话

我总是没法判断我想算法的正确性

可能是思维还不够吧

这题蛮好的

一道好题

区间DP决策的时候要分情况讨论

可以重,不要漏

Code of AC:

#include<bits/stdc++.h>
using namespace std;
int A[510],B[510],C[510],DP[510][510];
int main(){
    int n,T;
    cin>>T;
    for(int flag=1;flag<=T;++flag){
        string s;
        cin>>s;
        memset(DP,0,sizeof DP);
        memset(A,0,sizeof A);
        memset(B,0,sizeof B);
        memset(C,0,sizeof C);
        int tot=0;
        n=s.length();
        for(int i=1;i<=n;++i) A[i]=s[i-1]-'0'+1;
        for(int i=1;i<=n;++i){
            if(A[i]!=B[tot]) B[++tot]=A[i],++C[tot];
            else ++C[tot];
        }
//        for(int i=1;i<=tot;++i) cout<<B[i]<<" "<<C[i]<<endl; 
        for(int i=1;i<=tot;++i){ DP[i][i]=1;if(C[i]==1) DP[i][i]=2;}
        for(int i=1;i<tot;++i){
            for(int j=1;j+i<=tot;++j){
                DP[j][i+j]=1<<30;
                for(int k=j;k<i+j;++k) DP[j][i+j]=min(DP[j][i+j],DP[j][k]+DP[k+1][i+j]);
                if(B[j]==B[j+i]){
                    if(C[j]+C[i+j]>=3) DP[j][i+j]=min(DP[j][i+j],DP[j+1][i+j-1]);
                    if(C[j]==1||C[i+j]==1){
                        for(int k=j+1;k<i+j;++k){
                            if(B[k]==B[j]&&C[k]==1) DP[j][i+j]=min(DP[j][i+j],DP[j+1][k-1]+DP[k+1][i+j-1]);
                        }
                        DP[j][i+j]=min(DP[j][i+j],1+DP[j+1][i+j-1]);
                    }
                }
            }
        }
//        for(int i=1;i<=tot;++i){
//        	for(int j=1;j<=tot;++j)
//        		cout<<DP[i][j]<<" ";
//        	cout<<endl;
//		}
        cout<<"Case #"<<flag<<": "<<DP[1][tot]<<endl;
    }
}
//17 0 1 0 0 1 1 0 1 0 1 1 0 0 1 1 0 0

猜你喜欢

转载自blog.csdn.net/gipsy_danger/article/details/80572605