/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 2ms 540.0 KiB
#2 Accepted 5ms 588.0 KiB
#3 Accepted 175ms 872.0 KiB
#4 Accepted 286ms 664.0 KiB
#5 Accepted 426ms 101.941 MiB
#6 Accepted 411ms 101.77 MiB
#7 Accepted 387ms 51.246 MiB
#8 Accepted 386ms 10.754 MiB
#9 Accepted 291ms 584.0 KiB
#10 Accepted 153ms 796.0 KiB
#11 Accepted 415ms 102.0 MiB
#12 Accepted 421ms 101.758 MiB
#13 Accepted 424ms 101.938 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;
    s2 = "aeiouaeiou";
    map<char, int> mp = {{'a', 0}, {'e', 1}, {'i', 2}, {'o', 3}, {'u', 4}};
    vector<vector<int>> pos(5);
    for(int i = 0; i < s2.size(); i++) {
        pos[mp[s2[i]]].pb(i);
    }
    vector<vector<int>> pre(5, vector<int> (n + 1, 0));
    s1 = '#' + s1; // 1'base
    for(int ch = 0; ch < 5; ch++) {
        for(int i = 1; i <= n; i++) {
            auto it = lower_bound(all(pos[ch]), pos[mp[s1[i]]][0]);
            pre[ch][i] = pre[ch][i - 1] + (*it - pos[mp[s1[i]]][0]);
        }
        //cout << ch << " => "; coutall(pre[ch]);
    }

    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 = pre[back][i] - pre[back][i - 1] + self(self, i + 1, back, vis);
        for(int ch = 0; ch < 5; ch++) {
            if(vis[ch]) continue;
            vis[ch] = 1;
            ans = min(ans, pre[ch][i] - pre[ch][i - 1] + self(self, i + 1, ch, vis));
            int x = pre[ch][i] - pre[ch][i - 1] + 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 16:59:10
Judged At
2024-12-08 18:30:50
Judged By
Score
100
Total Time
426ms
Peak Memory
102.0 MiB