Coloring "CQOI2007"

The meaning of problems

Given a string of empty string in claim dyeing, to become the target string. Each time interval for a continuous dyeing can be dyed the same color for the Minimum frequency


Thinking

A typical interval dp title.

Substate \ (f [i] [j ] \) represents the minimum interval scheme. Obviously the number of single-point solution for 1.

If a section is about the same as the endpoint, it can be understood as the whole interval beginning staining, then copy [l + 1, r] in the staining pattern.

If different, then it is the breakpoint enumeration, statistical minimum interval.

Code

#include <bits/stdc++.h>

using namespace std;

namespace StandardIO {

    template<typename T>inline void read (T &x) {
        x=0;T f=1;char c=getchar();
        for (; c<'0'||c>'9'; c=getchar()) if (c=='-') f=-1;
        for (; c>='0'&&c<='9'; c=getchar()) x=x*10+c-'0';
        x*=f;
    }

    template<typename T>inline void write (T x) {
        if (x<0) putchar('-'),x*=-1;
        if (x>=10) write(x/10);
        putchar(x%10+'0');
    }

}

using namespace StandardIO;

namespace Project {

    const int N=55;
    
    int n;
    string s;
    int f[N][N];

    inline void MAIN () {
        cin>>s,n=s.length();
        memset(f,0x3f,sizeof(f));
        for (register int i=0; i<n; ++i) f[i][i]=1;
        for (register int len=2; len<=n; ++len) {
            for (register int l=0,r=len-1; r<n; ++l,++r) {
                if (s[l]==s[r]) f[l][r]=min(f[l+1][r],f[l][r-1]);
                else {
                    for (register int k=l; k<r; ++k) {
                        f[l][r]=min(f[l][r],f[l][k]+f[k+1][r]);
                    }
                }
            }
        }
        write(f[0][n-1]);
    }
    
}

int main () {
//  freopen(".in","r",stdin);
//  freopen(".out","w",stdout);
    Project::MAIN();
}

Guess you like

Origin www.cnblogs.com/ilverene/p/11609933.html