hdu 区间dp 2476

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int INF=1<<29;
const int maxn=102;
int dp[maxn][maxn],ans[maxn];
char s1[maxn],s2[maxn];
int main()
{
  //  freopen("//media/学习/ACM/input.txt","r",stdin);
    while(scanf("%s%s",s1,s2)!=EOF)
    {
        int i,j,k,len=strlen(s1);
      memset(dp,0,sizeof(dp));
        for (int i=0; i<len; i++) dp[i][i]=1;
        for (int j=0; j<len; j++) {///把s2 弄成同一种颜色的区间操作
            for (int i=j-1; i>=0; i--) {
                dp[i][j]=dp[i+1][j]+1;
                for (int k=i+1; k<=j; k++) {
                    if (s2[i]==s2[k]) {
                        dp[i][j]=min(dp[i][j],dp[i+1][k]+dp[k+1][j]);
                    }
                }
            }
        }
      //  cout<<"换成一种颜色 需要多长时间:"<<dp[len-1][]<<endl;
        for (int i=0; i<len; i++) {
            ans[i]=dp[0][i];
            if (s1[i]==s2[i]) {
                if (i==0) ans[i]=0;
                else ans[i]=ans[i-1];
            }
            else {
                for (int j=0; j<i; j++)
                    ans[i]=min(ans[i],ans[j]+dp[j+1][i]);
            }
        }
        printf("%d\n", ans[len-1]);
    }
    return 0;
}

你可能感兴趣的:(hdu 区间dp 2476)