1 solutions

  • 0
    @ 2026-11-13 23:44:47

    C++ :

    #include <bits/stdc++.h>
    #define N 100005
    using namespace std;
    int dp[N],a[25];
    string d[25];
    int main(){
        int n,m;
        string s;
        cin >> n;
        for (int i = 1;i<=n;i++) {
            cin >> a[i];
            for (int j = 1;j<=i;j++)
                    d[i] += "abc";        
        }
        
        cin >> m >> s;
        s = " "  + s;
        for (int i = 3;i<=m;i++){
            dp[i] = dp[i-1];
            for (int j = 1;j<=n;j++){
                int pos = i - d[j].size() + 1;
                if (pos < 0) break;
                if (s.substr(pos,d[j].size()) == d[j])
                    dp[i] = max(dp[i],dp[pos] + a[j]);
                else break;
            }
        }
        
        cout << dp[m];
        return 0;
    }
    
    • 1

    Information

    ID
    128
    Time
    1000ms
    Memory
    500MiB
    Difficulty
    (None)
    Tags
    # Submissions
    0
    Accepted
    0
    Uploaded By