跳到正文

题解

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

wangyizhou0109约 3 分钟
原文
文章信息与版本

文章信息

发布
2026年2月28日
收录
2026年2月28日
更新
2026年9月23日
篇幅
约 3 分钟
获赞
1
题目
P15432

版本

  1. v2当前
  2. v1

思路

可以先统计大写,小写,数字它们的个数。 其中O,o,0单独统计,最后求出答案即可。

详见代码~~

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n;
string s;
signed main(){
	cin>>n;
    while(n--)
    {
        int suma=0;//表示字符串中有多少个大写子母
		int sumb=0;//表示字符串中有多少个小写子母
		int sumc=0;//表示字符串中有多少个数字
		int sum=0;//表示字符串中有多少个可用替换的O,o,0
		int cntO=0;//表示字符串中有多少个O
		int cnto=0;//表示字符串中有多少个o
		int cnt0=0;//表示字符串中有多少个0
		int ans=0;//统计修改次数
        cin>>s;
        for(int i=0;i<s.size();i++){
            if('A'<=s[i]&&s[i]<='Z'){
                //将O单独统计
                if(s[i]=='O'){
                     cntO++;
                }
                else{
                    suma++;
                }
            }
            if('a'<=s[i]&&s[i]<='z'){
                //将o单独统计
                if(s[i]=='o'){
                    cnto++;
                }
                else{
                    sumb++;
                }
            }
            if('0'<=s[i]&&s[i]<='9'){
                //将0单独统计
                if(s[i]=='0'){
                    cnt0++;
                }
                else{
                    sumc++;
                }
            }
        }
        if(suma==0){
            if(cntO==0){
                ans++;
            }
            else{
        		cntO--;
            }
        }
        if(sumb==0){
            if(cnto==0){
                ans++;
            }
            else{
                cnto--;
            }
        }
        if(sumc==0){
            if(cnt0==0){
                ans++;
            }
            else{
                cnt0--;
            }
        }
        sum=cntO+cnto+cnt0;
        if(sum>=ans){
        	cout<<ans<<endl;
		}
		else{
			cout<<-1<<endl;
		}
    }
	return 0;
}
原文