cppAcademy
FROM LEARNING TO COMPETING

Beginner.
Your first challenges.

Bulgarian Informatics Group E · 2007–2026

270 independently explained problems: 54 Municipal, 54 Regional, 105 National Olympiad tasks and 57 Autumn Tournament tasks. Start with the easier tasks and work upwards. Difficulty labels are learning estimates, not official ratings; Group E also includes challenging national-level problems. Years follow Arena’s competition-season labels. Source clarifications are noted on affected tasks.
GROUP E2017 · EasyStrings & scoring

Choose the Stronger Password

National Olympiad — Municipal round (2017 season)

Submission name: password.cpp

Problem summary

Compare two four-character passwords. Lowercase letters score their alphabet positions 1–26, uppercase letters twice their positions, and digits their numeric values. Print the password with the larger total and that total; print 0 if tied.

Input

Two four-character strings on separate lines.

Output

The stronger password, a space and its score, or 0 on a tie.

Constraints

Exactly four characters per password, each a Latin letter or decimal digit.

Worked example

An independently prepared example for this explanation.

Input

Ac59
qv71

Output

qv71 47

Solution analysis

  1. Assign a weight by character range: c − a + 1 for lowercase letters, twice c − A + 1 for uppercase letters, and c − 0 for decimal digits.
  2. Sum these four weights independently for both strings. Compare the totals, preserving the original strings so that the winning password can be printed exactly as entered.
  3. Ac59 scores 2 + 3 + 5 + 9 = 19. qv71 scores 17 + 22 + 7 + 1 = 47, so the second password wins and the output is qv71 47.

Why it works

The character-range branches implement exactly the three weight rules. Adding all four contributions gives each specified password score. Comparing them selects the uniquely stronger password, or detects equality and prints the required zero.

Complexity: O(L) time for total input length L = 8, and O(L) space to preserve the strings.

Watch out. Alphabet positions start at one. Uppercase scores are doubled. A tie prints only 0, not either password.

Commented C++17 solution

An independently written complete program with standard headers.

#include <iostream>
#include <string>
using namespace std;
int score(const string &password) {
    int total = 0;
    // Character ranges select the three distinct scoring rules.
    for (char c : password) {
        if (c >= 'a' && c <= 'z') {
            total += c - 'a' + 1;
        } else if (c >= 'A' && c <= 'Z') {
            total += 2 * (c - 'A' + 1);
        } else {
            total += c - '0';
        }
    }
    return total;
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    string a, b;
    cin >> a >> b;
    int first = score(a);
    int second = score(b);
    if (first == second) {
        cout << 0 << '\n';
    } else if (first > second) {
        cout << a << ' ' << first << '\n';
    } else {
        cout << b << ' ' << second << '\n';
    }
    return 0;
}

Source: Arena Olimpiici ↗