Minimum Time to Type a String with Special Keys

Your computer has a keyboard with three keys: 'a' key, Shift key, and Caps Lock key. The Caps Lock key has a light on it. Initially, the light on the Caps Lock key is off, and the screen shows an empty string.

You can do the following three actions any number of times in any order:

  1. Spend X milliseconds to press only the 'a' key. If the light on the Caps Lock key is off, 'a' is appended to the string on the screen; if it is on, 'A' is.
  2. Spend Y milliseconds to press the 'a' key and Shift key simultaneously. If the light on the Caps Lock key is off, 'A' is appended to the string on the screen; if it is on, 'a' is.
  3. Spend Z milliseconds to press the Caps Lock key. If the light on the Caps Lock key is off, it turns on; if it is on, it turns off.

Given a string S consisting of 'A' and 'a', determine at least how many milliseconds you need to spend to make the string shown on the screen equal to S.

C++ Code:

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

int main() {
    int x, y, z;
    string s;
    cin >> x >> y >> z >> s;

    bool caps_lock = false; // initial state of the Caps Lock key
    int time = 0; // total time spent
    for (char c : s) { // process each character in the input string
        if (c == 'a') {
            if (caps_lock) { // if Caps Lock is on, press A
                time += y;
            } else { // if Caps Lock is off, press a
                time += x;
            } 
        } else { // if c == 'A'
            if (caps_lock) { // if Caps Lock is on, press a
                time += x;
            } else { // if Caps Lock is off, press A
                time += y;
            } 
        } 
    } 

    cout << time << endl; // output the total time spent

    return 0;
}
Minimum Time to Type a String with Special Keys

原文地址: https://www.cveoy.top/t/topic/ojeU 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录