跳到正文

题解

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

_zylenox_约 2 分钟
原文
文章信息与版本

文章信息

发布
2026年2月25日
收录
2026年2月25日
更新
2026年11月17日
篇幅
约 2 分钟
题目
P15432

版本

  1. v1当前

题目大意

在 nn 个字符串中,互相交换 OO、oo 和 00 三种字符,使得整个字符串中至少包含一个小写字母、一个大写字母和一个数字。

思路

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

代码

C++
#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;
}
原文