萌新求助滚动数组做法
查看原帖
萌新求助滚动数组做法
317650
lao_lihiOI楼主2021/8/4 16:41
#include <bits/stdc++.h>
#define rep(a, b, c) for(register int a = b; a <= c; ++ a)
#define Rrep(a, b, c) for(register int a = b; a >= c; -- a)
#ifndef LLZYYDS
#define getchar() p1 == p2 && (p2 = (p1 = buf1) + fread(buf1, 1, 1024, stdin), p1 == p2) ? EOF : *p1++;
#endif
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int MAXN = 100005, inf = 0x3f3f3f3f;
char *p1, *p2, buf1[1024], buf2[32];
inline void End() {
#ifdef LLZYYDS
    puts(""); system("pause");
#endif
    exit(0);
}
template<class T> inline void read(T &x) {
    x = 0; int f = 1; char ch = getchar();
    while (!isdigit(ch)) { if(ch == '-') f = -1; ch = getchar(); }
    while (isdigit(ch)) { x = x * 10 + (ch ^ 48); ch = getchar(); }
    (f == -1) ? (x = -x) : 1;
}
template<class T> inline void write(T x) {
    int cnt = 0;
    if(x < 0) { putchar('-');  x = -x; }
    do { buf2[++ cnt] = x % 10 + '0'; x /= 10; } while(x);
    while(cnt) putchar(buf2[cnt --]);
}
template<class T> inline T max(T &x, T &y) { return x > y ? x : y; }
template<class T> inline T min(T &x, T &y) { return x < y ? x : y; }
template<class T> inline T abs(T &x) { return x > 0 ? x : -x; }

int n, k;
int res[53];
string s;
string f[2][45];
//f[i][j]: 前j个数字放入i个乘号的最大乘积

inline string mul(string a, string b) {
    if(a == "0" || b == "0") return "0";
    if(a == "1") return b;
    if(b == "1") return a;
    int la = a.length(), lb = b.length();
    memset(res, 0, sizeof(res));
    reverse(a.begin(), a.end());
    reverse(b.begin(), b.end());
    rep(i, 0, la - 1)
        rep(j, 0, lb - 1) {
            res[i + j] += (a[i] - '0') * (b[j] - '0');
            res[i + j + 1] += res[i + j] / 10;
            res[i + j] %= 10;
        }
    int f = 0;
    string t;
    Rrep(i, 42, 0) {
        if(res[i]) f = 1;
        if(f) t += res[i] + '0';
    }
    return t;
}
inline string Max(string a, string b) {
    int la = a.length(), lb = b.length();
    if(la > lb) return a;
    else if(lb > la) return b;
    else return (a >= b) ? a : b;
}
int main() {
    cin >> n >> k;
    cin >> s;
    rep(i, 1, n) f[0][i] = s.substr(0, i);
    
    //f[i][j] = max{f[i - 1][h] * s(h...j)}
    rep(i, 1, n)
        rep(j, 1, n)
            rep(h, 1, j - 1) f[i & 1][j] = Max(f[i & 1][j], mul(f[(i - 1) & 1][h], s.substr(h, j - h)));
    cout << f[k & 1][n];
    End();
}

萌新刚学DP,普通做法AC,滚动数组WA,求大佬赐教。

2021/8/4 16:41
加载中...