Submission #1096892

#TimeUsernameProblemLanguageResultExecution timeMemory
1096892thangdz2k7Copy and Paste 3 (JOI22_copypaste3)C++17
100 / 100
241 ms110296 KiB
// author : thembululquaUwU
// 3.9.2024

#include <bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
#define endl '\n'

using namespace std;
using ll = long long;
using ii = pair <int, int>;
using vi = vector <int>;

const int N = 3e3;
const int mod = 1e9 + 7;

void maxl(auto &a, auto b) {a = max(a, b);}
void minl(auto &a, auto b) {a = min(a, b);}

#define next __next__

int n, a, b, c, f[N][N], next[N][N];
ll dp[N][N];
string s;

void solve(){
    cin >> n >> s; s = ' ' + s;
    cin >> a >> b >> c;
    for (int i = n; i >= 1; -- i){
        for (int j = n; j > i; -- j) if (s[i] != s[j]) f[i][j] = 0; else f[i][j] = f[i + 1][j + 1] + 1;
    }

    for (int i = 0; i <= n + 1; ++ i){
        for (int j = 0; j <= n + 1; ++ j){
            if (i > j) dp[i][j] = 0;
            else dp[i][j] = ll(a) * (j - i + 1);
            next[i][j] = n + 2;
        }
    }

    for (int i = 1; i <= n; ++ i) {
        int jj = i + 1;
        for (int j = i; j <= n; ++ j){
            if (jj > n) break;
            if (jj <= j || f[i][jj] < (j - i + 1)){
                -- j; ++ jj;
                continue;
            }
            next[i][j - i + 1] = jj;
        }
    }

    for (int i = n; i >= 1; -- i){
        for (int j = i; j <= n; ++ j){
            minl(dp[i][j], min(dp[i + 1][j], dp[i][j - 1]) + a);
            int p = i, le = j - i + 1, d = 1;
            while (p < n + 1){
                ++ d; p = next[p][le]; int r = p + le - 1;
                ll cost = ll(d) * c + b + ll(r - i + 1 - d * le) * a;
                minl(dp[i][r], dp[i][j] + cost);
            }
        }
    }

    cout << dp[1][n];
}

int main(){
    if (fopen("pqh.inp", "r")){
        freopen("pqh.inp", "r", stdin);
        freopen("pqh.out", "w", stdout);
    }
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    int t = 1; // cin >> t;
    while (t --) solve();
    return 0;
}

Compilation message (stderr)

copypaste3.cpp:18:11: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
   18 | void maxl(auto &a, auto b) {a = max(a, b);}
      |           ^~~~
copypaste3.cpp:18:20: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
   18 | void maxl(auto &a, auto b) {a = max(a, b);}
      |                    ^~~~
copypaste3.cpp:19:11: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
   19 | void minl(auto &a, auto b) {a = min(a, b);}
      |           ^~~~
copypaste3.cpp:19:20: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
   19 | void minl(auto &a, auto b) {a = min(a, b);}
      |                    ^~~~
copypaste3.cpp: In function 'int main()':
copypaste3.cpp:71:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   71 |         freopen("pqh.inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
copypaste3.cpp:72:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   72 |         freopen("pqh.out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...