/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 532.0 KiB
#2 Accepted 4ms 532.0 KiB
#3 Accepted 123ms 764.0 KiB
#4 Accepted 260ms 832.0 KiB
#5 Accepted 551ms 255.988 MiB
#6 Accepted 554ms 255.91 MiB
#7 Accepted 468ms 128.316 MiB
#8 Accepted 393ms 26.02 MiB
#9 Accepted 250ms 532.0 KiB
#10 Accepted 129ms 832.0 KiB
#11 Accepted 556ms 255.844 MiB
#12 Accepted 556ms 255.984 MiB
#13 Accepted 524ms 255.988 MiB

Code

#include <bits/stdc++.h>
#define int long long
#define endll '\n';
#define pb push_back
#define all(v) v.begin(), v.end()
using namespace std;

const int mod = 1e9 + 7, N = 1e6;

char ar[5] = {'a', 'e', 'i', 'o', 'u'};
string s;
int n;
map<char, int> mp;
int dp[100000 + 2][5][1 << 5 + 1];
int rec(int i, int ps, int mask)
{
    if (i == n)
        return 0;

    if(dp[i][ps][mask] != -1) return dp[i][ps][mask];

    int ans = 1e9;
    char ch = ar[ps], nw = s[i];
    int c = 0;
    while (nw != ch)
    {
        nw = mp[nw];
        c++;
    }

    ans = min(ans, rec(i + 1, ps, mask) + c);
    for (int j = 0; j < 5; j++)
    {
        if (mask & (1 << j))
        {
            ans = min(ans, rec(i + 1, j, mask ^ (1 << j)) + c);
        }
    }

    return dp[i][ps][mask] = ans;
}

void solve()
{
    mp['a'] = 'e';
    mp['e'] = 'i';
    mp['i'] = 'o';
    mp['o'] = 'u';
    mp['u'] = 'a';
    cin >> n;
    cin >> s;
    for(int  i = 0; i <= n; i++) {
        for(int j = 0; j < 5; j++) {
            for(int k = 0; k <= (1 << 5); k++) {
                dp[i][j][k] = -1;
            }
        }
    }
    int ans = 1e18;
    for (int i = 0; i < 5; i++)
    {
        ans = min(ans, rec(0, i, ((1 << 5) - 1) ^ (1 << i)));
    }
    cout << ans << endll;
}

int32_t main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t = 1;
    cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}

Information

Submit By
Type
Submission
Problem
P1140 Vowel arrangement
Language
C++17 (G++ 13.2.0)
Submit At
2024-12-26 04:12:07
Judged At
2024-12-26 04:12:07
Judged By
Score
100
Total Time
556ms
Peak Memory
255.988 MiB