Codeforces 938F Erasing Substrings (看题解) dp

Erasing Substrings

感觉这种写法想不到啊。。。

dp[ i ][ j ] 表示选了 i 个, 删了的情况为 j 是否为字典序最小的串。

因为如果dp[ i ][ j ] 的字典序比 dp[ i ][ k ]的字典序小, dp[ i ][ k ] 就没有用了。

还需要sos dp 去更新状态。

#include<bits/stdc++.h>
#define LL long long
#define LD long double
#define ull unsigned long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ALL(x) (x).begin(), (x).end()
#define fio ios::sync_with_stdio(false); cin.tie(0);

using namespace std;

const int N = 5000 + 7;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 998244353;
const double eps = 1e-8;
const double PI = acos(-1);

template<class T, class S> inline void add(T& a, S b) {a += b; if(a >= mod) a -= mod;}
template<class T, class S> inline void sub(T& a, S b) {a -= b; if(a < 0) a += mod;}
template<class T, class S> inline bool chkmax(T& a, S b) {return a < b ? a = b, true : false;}
template<class T, class S> inline bool chkmin(T& a, S b) {return a > b ? a = b, true : false;}

int n, m, len, tot;
char s[N], ans[N];
bool dp[N][1 << 12];

int main() {
    scanf("%s", s + 1);
    n = strlen(s + 1);
    m = log2(n);
    len = n - (1 << m) + 1;
    for(int i = 0; i < (1 << m); i++) dp[0][i] = true;
    for(int i = 1; i <= len; i++) {
        for(int mask = 0; mask < (1 << m); mask++)
            dp[i][mask] = dp[i - 1][mask];
        char c = 'z' + 1;
        for(int mask = 0; mask < (1 << m); mask++)
            if(dp[i - 1][mask]) chkmin(c, s[i + mask]);
        for(int mask = 0; mask < (1 << m); mask++)
            if(dp[i][mask] && s[i + mask] != c) dp[i][mask] = 0;
        for(int mask = 0; mask < (1 << m); mask++)
            for(int j = 0; j < m; j++)
                if(mask >> j & 1) dp[i][mask] |= dp[i][mask ^ (1 << j)];
        ans[tot++] = c;
    }
    ans[tot] = '\0';
    puts(ans);
    return 0;
}

/*
*/

猜你喜欢

转载自www.cnblogs.com/CJLHY/p/10941384.html