/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 2ms 540.0 KiB
#2 Accepted 4ms 540.0 KiB
#3 Accepted 117ms 488.0 KiB
#4 Accepted 236ms 908.0 KiB
#5 Accepted 362ms 175.984 MiB
#6 Accepted 368ms 176.012 MiB
#7 Accepted 320ms 88.309 MiB
#8 Accepted 345ms 18.281 MiB
#9 Accepted 195ms 820.0 KiB
#10 Accepted 119ms 788.0 KiB
#11 Accepted 370ms 176.094 MiB
#12 Accepted 362ms 176.008 MiB
#13 Accepted 365ms 176.047 MiB

Code

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

int get_index(char ch) {
    string vowels = "aeiou";
    for(int i = 0; i < vowels.size(); i++) {
        if(vowels[i] == ch) {
            return i;
        }
    }
    assert(false);
}

// i -> j
int get_cost(int i, int j) {
    if(j >= i) return j - i;
    return j - i + 5;
}

int solve(string s) {
    int n = s.size();
    vector<int> v(n);
    for(int i = 0; i < n; i++) {
        v[i] = get_index(s[i]);
    }
    vector<vector<vector<int>>> dp(n, vector<vector<int>>(1<<5, vector<int>(5, 1e9)));
    for(int i = 0; i < n; i++) {
        for(int mask = 0; mask < (1<<5); mask++) {
            for(int j = 0; j < 5; j++) {
                if((mask >> j) & 1) {
                    continue;
                }
                // 
                int prev_best = (i > 0 ? dp[i - 1][mask][j] : 0);
                for(int k = 0; k < 5; k++) {
                    if((mask >> k) & 1) {
                        prev_best = min(prev_best, (i > 0 ? dp[i - 1][mask ^ (1<<k)][k] : 0));
                    }
                }
                dp[i][mask][j] = prev_best + get_cost(v[i], j);
            }
        }
    }

    int ans = 1e9;
    for(int mask = 0; mask < (1<<5); mask++) {
        for(int j = 0; j < 5; j++) {
            ans = min(ans, dp[n - 1][mask][j]);
        }
    }
    return ans;
}


int main() {
    int tc;
    cin >> tc;
    while(tc--) {
        int n;
        cin >> n;
        string s;
        cin >> s;
        cout << solve(s) << endl;
    }
}

Information

Submit By
Type
Submission
Problem
P1140 Vowel arrangement
Contest
LU Divisonal Contest Problem Testing Round
Language
C++17 (G++ 13.2.0)
Submit At
2024-12-07 20:35:54
Judged At
2024-12-08 18:32:06
Judged By
Score
100
Total Time
370ms
Peak Memory
176.094 MiB