Interval dp template

Keywords: iOS

2020.6.4
Now it's 2 hours to cf div2 and 2 days to my birthday. It's 20 now. I hope to get a nac ranking this year and go to the postgraduate entrance examination.

Well, when it comes to interval dp, I've written it several times before, but almost several times I've pushed out the equation of state transition obviously, but I'm not sure about the enumeration order. At that time, when I saw the topic of noip 2013 tg, I also exclaimed that the horizontal groove was so strong, how could I think of this enumeration order in the examination room. Later it was found that there was a template. Well, if you can't do it this time, forgive yourself. Record the enumeration order. Let's do the stone combination before atonement.

rep(i, 1, n){
        rep(l, 1, n-1){
            int r = l + i;
             rep(k ,l , r){
                    //Do the right thing
             }
        }
    }

Luogu p4170 cqoi (talking about Chongqing or hhhhh, a friend's stem)

#include <bits/stdc++.h>
using namespace std;
#define limit (5000 + 5)//Prevent spillage
#define INF 0x3f3f3f3f
#define inf 0x3f3f3f3f3f
#define lowbit(i) i&(-i)//One step, two steps
#define EPS 1e-6
#define FASTIO  ios::sync_with_stdio(false);cin.tie(0);
#define ff(a) printf("%lld\n",a );
#define pi(a,b) pair<a,b>
#define rep(i, a, b) for(int i = a; i <= b ; ++i)
#define per(i, a, b) for(int i = b ; i >= a ; --i)
#define mint(a,b,c) min(min(a,b), c)
#define MOD 998244353
#define FOPEN freopen("C:\\Users\\tiany\\CLionProjects\\acm_01\\data.txt", "rt", stdin)
typedef long long ll;
typedef unsigned long long ull;
int dp[limit][limit];
ll read(){
    ll sign = 1, x = 0;char s = getchar();
    while(s > '9' || s < '0' ){if(s == '-')sign = -1;s = getchar();}
    while(s >= '0' && s <= '9'){x = x * 10 + s - '0';s = getchar();}
    return x * sign;
}//Fast reading
void write(ll x){
    if(x < 0) putchar('-'),x = -x;
    if(x / 10) write(x / 10);
    putchar(x % 10 + '0');
}
int n;
char str[limit<<1];
int main() {
#ifdef LOCAL
    FOPEN;
#endif
    scanf("%s" , str + 1);
    n = strlen(str + 1);
    memset(dp, INF, sizeof(dp));
    rep(i ,1, n){
        dp[i][i] = 1;//It's all one
    }
    rep(i, 1, n){
        rep(l, 1, n-1){
            int r = l + i;
            if(str[l] == str[r]){
                dp[l][r] = min(dp[l + 1][r] , dp[l][r - 1]);
            }else{
                rep(k ,l , r){
                    dp[l][r] = min(dp[l][r], dp[l][k] + dp[k + 1][r]);
                }
            }
        }
    }
    write(dp[1][n]);
    return 0;
}

Stone merge:
Our main gun failed to penetrate the enemy's armor lol. I have to figure out what happened.

Posted by php_guest on Thu, 04 Jun 2020 09:49:14 -0700