/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 768.0 KiB
#2 Accepted 8ms 540.0 KiB
#3 Accepted 297ms 608.0 KiB
#4 Accepted 608ms 668.0 KiB
#5 Accepted 624ms 89.895 MiB
#6 Accepted 609ms 89.645 MiB
#7 Accepted 607ms 50.289 MiB
#8 Accepted 608ms 10.535 MiB
#9 Accepted 574ms 588.0 KiB
#10 Accepted 318ms 752.0 KiB
#11 Accepted 605ms 89.559 MiB
#12 Accepted 607ms 89.539 MiB
#13 Accepted 477ms 89.73 MiB

Code

#include<bits/stdc++.h>
#define endl        '\n'
#define F           first
#define S           second
#define pb          push_back
#define yes         cout<<"YES\n"
#define no          cout<<"NO\n"
#define all(x)      x.begin(),x.end()
#define allr(x)     x.rbegin(),x.rend()
#define error1(x)   cerr<<#x<<" = "<<(x)<<endl
#define error2(a,b) cerr<<"("<<#a<<", "<<#b<<") = ("<<(a)<<", "<<(b)<<")\n";
#define coutall(v)  for(auto &it: v) cout << it << " "; cout << endl;
using namespace std;
using ll = long long;
using ld = long double;

const int inf = 1e9;

void solve() {
    int n;
    string s1, s2;
    cin >> n >> s1;
    s1 = '#' + s1; // 1'base

    s2 = "aeiouaeiou";
    map<char, short> mp = {{'a', 0}, {'e', 1}, {'i', 2}, {'o', 3}, {'u', 4}};
    vector<vector<short>> pos(5);
    for(short i = 0; i < s2.size(); i++) {
        pos[mp[s2[i]]].pb(i);
    }
    
    auto cost = [&](short from, short to) -> short {
        return *lower_bound(all(pos[to]), pos[from][0]) - pos[from][0];
    };

    vector<vector<vector<int>>> dp(6, vector<vector<int>> (33, vector<int> (n + 1, -1)));
    auto rec = [&](auto&& self, int i, short back, bitset<5> &vis) -> int {
        if(i > n) return 0;
        int &ans = dp[back][vis.to_ulong()][i];
        if(ans != -1) return ans;
        ans = inf;
        if(back != 5) ans = cost(mp[s1[i]], back) + self(self, i + 1, back, vis);
        for(short ch = 0; ch < 5; ch++) {
            if(vis[ch]) continue;
            vis[ch] = 1;
            ans = min(ans, cost(mp[s1[i]], ch) + self(self, i + 1, ch, vis));
            vis[ch] = 0;
        }
        return ans;
    };

    bitset<5> vis = 0;
    cout << rec(rec, 1, 5, vis) << endl;
    return;
}

int32_t main() {
    ios::sync_with_stdio(false); cin.tie(0);
    int tc = 1;
    cin >> tc;
    for (int t = 1; t <= tc; t++) {
        // cout << "Case " << t << ": ";
        solve();
    }
    return 0;
}

Information

Submit By
Type
Submission
Problem
P1140 Vowel arrangement
Language
C++17 (G++ 13.2.0)
Submit At
2024-11-27 17:23:23
Judged At
2024-12-08 18:30:58
Judged By
Score
100
Total Time
624ms
Peak Memory
89.895 MiB