题解:P15432 [蓝桥杯 2025 国 Python B] 修改密码

· · 题解

题目大意

在 n 个字符串中,互相交换 O、o 和 0 三种字符,使得整个字符串中至少包含一个小写字母、一个大写字母和一个数字。

思路

先算出小写字母、大写字母、数字的个数和三种字符分别的个数,再判断三种字符调动是否合法,最后输出。

代码

#include <bits/stdc++.h>
using namespace std;
int f(string t) {
    int sO = 0, so = 0, s0 = 0;
    int s = 0, S = 0, num = 0;
    for (int i = 0; i < t.size(); i++) {
        if (t[i] >= 'A' && t[i] <= 'Z') S++;
        if (t[i] >= 'a' && t[i] <= 'z') s++;
        if (t[i] >= '0' && t[i] <= '9') num++;
        if (t[i] == 'O') sO++;
        if (t[i] == 'o') so++;
        if (t[i] == '0') s0++;
    }
    S -= sO;
    s -= so;
    num -= s0;
    if (sO != 0 && S == 0) S++, sO--;
    if (so != 0 && s == 0) s++, so--;
    if (s0 != 0 && num == 0) num++, s0--;
    int d = 0;
    if (S == 0) d++;
    if (s == 0) d++;
    if (num == 0) d++;
    if (d <= sO + so + s0) return d;
    else return -1;
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int T;
    cin >> T;
    while (T--) {
        string s;
        cin >> s;
        cout << f(s) << "\n";
    }
    return 0;
}