codeforce problem 864/B 自己写出来的,附带详细的解释

博客给出了Codeforces题目链接,展示了相关代码。代码主要实现了统计非0数值个数及处理字符串中大小写字母的功能,通过一系列操作找出小写字母区间内不同字母的最大数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

链接:   http://codeforces.com/problemset/problem/864/B
题目截图如下:

 

代码放在下面:

 

 

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

int count_nums_not_0(int a[]){     //返回非0的数值有几个,即多少个字母 
    int count = 0;
    for(int i = 1;i <= 26 ;i++){
        if(a[i]){
            count++;
        }
    }
    return count;
}
int main(){
    int n,nums[30],record_index[205];
    char str[205];
    while(scanf("%d",&n) != EOF && n){
        int maxx = 0;
        getchar();
        gets(str);
        memset(record_index,0,sizeof(record_index));
        int k = 1;record_index[0] = 0;
        for(int i = 0;i < n;i++){
            if(str[i] >= 'A' && str[i] <= 'Z'){          //记录大写字母的下标,下标为0对应着str[0]的下标 
                record_index[k++] = i;
            }
        }    //k is the number of the upperStr
        record_index[k] = n;                              // str[n],nums[]数组一共有 k+2个数值 
        for(int i = 0;i < (k-1+2);i++){                       //i遍历到 nums[]的倒数第二个就好了 
            memset(nums,0,sizeof(nums));
            for(int j = record_index[i];j < record_index[i+1];j++){            
                //printf("uppe str(a,b) is %c\n",str[j]);     //对的 
                nums[(str[j] - 'a' + 1)] += 1;            //核心代码,对小写区间进行处理,用nums[]数组记录26个字母有多少个  
            }
            maxx = max(maxx,count_nums_not_0(nums));     //maxx is the ans. 
        }
        printf("%d\n",maxx);
    }
}

没有了,谢谢,2019.3.21 大一下学期

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值