/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Wrong Answer 1ms 796.0 KiB
#2 Wrong Answer 7ms 788.0 KiB

Code

#include <bits/stdc++.h>
using namespace std;

#define ll long long int
#define nl '\n'
#define INF 1e8

string v = "aeiou";
map<pair<char, char>, int> cost;
map<char, int> id;

int n;
string st;
vector<vector<vector<int>>> dp;

int f(int i, int mask, char lst) {
    if (i == n) return 0;

    int j = mask, k = id[lst];
    if (dp[i][j][k] != -1) return dp[i][j][k];

    int take = INF, not_take = INF;

    // Option 1: Don't take a vowel
    if (!(mask & (1 << id[st[i]]))) {
        if (lst != '*' && st[i] != lst) mask |= (1 << id[lst]);
        not_take = f(i + 1, mask, st[i]);
    }

    // Option 2: Replace with a vowel
    for (auto &c : v) {
        int t_mask = mask;
        if (!(t_mask & (1 << id[c]))) {
            if (lst != '*' && c != lst) t_mask |= (1 << id[lst]);
            take = min(take, cost[{st[i], c}] + f(i + 1, t_mask, c));
        }
    }

    return dp[i][j][k] = min(take, not_take);
}

void answer_to_the_question() {
    cin >> n >> st;
    dp = vector(n + 2, vector(33, vector(6, -1)));

    cout << f(0, 0, '*') << nl;
}

int32_t main() {
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    int test_case;
    cin >> test_case;

    // Precompute costs and mappings
    for (int i = 0; i < 5; i++) {
        for (int j = 0; j < 5; j++) {
            cost[{v[i], v[j]}] = (10 + j - i) % 5;
        }
    }
    for (int i = 0; i < 5; i++) id[v[i]] = i;
    id['*'] = 5;

    for (int MJS = 1; MJS <= test_case; MJS++) {
        answer_to_the_question();
    }

    return 0;
}

Information

Submit By
Type
Submission
Problem
P1140 Vowel arrangement
Language
C++17 (G++ 13.2.0)
Submit At
2024-12-12 08:34:21
Judged At
2024-12-12 08:34:21
Judged By
Score
0
Total Time
7ms
Peak Memory
796.0 KiB