Polycarp and Letters 题解 | ACM

题目大意已知一个只包含大写字母和小写字母的字符串s.现在有这样一个集合:它可以包含字符串中不同小写字母的位置,但是这些位置中任意两个之间不能有大写字母.现在请你写一个程序求集合最大时的大小. 输入: 第一行一个整数n(1<=n<=200),表示字符串的长度 第二行一个字符串s 输出: 这个集合最大时的大小
输入数据The first line contains a single integer n n ( 1<=n<=200 1<=n<=200 ) — length of string s s .The second line contains a string s s consisting of lowercase and uppercase Latin letters.
数据输出Print maximum number of elements in pretty set of positions for string s s .
样例11aaaaBaabAbA12zACaAbbaazzC3ABC
样例输出230

##思路简述

  • 一个数组,0~25分别代表26个英文字母;
  • 先遍历字符串,从大写字母开始计数
  • 当大写字母之后出现小写字母,开始按照出现的字母进行计数,当计数的时候对应的数组元素值为0,记录这是一个新的字母。
  • 如果被大写字母中断,则初始化数组。

因此AC代码为:

#include<stdio.h>
#include<string.h>
int main()
{
	int ch[30];
	char s[205];
	int length;
	int max,count;
	while(~scanf("%d",&length))
	{
		scanf("%s",s);
		max = 0;
		for(int i = 0;i < length;i++)
		{
			if(s[i] >= 'a' && s[i] <= 'z')
			{
				count = 0;
				memset(ch,0,sizeof(ch));
				for(int j = i;s[j] >= 'a' && s[j] <= 'z';j++)
				{
					if(ch[s[j]-'a'] == 0)
					{
						count++;
						ch[s[j]-'a']++;
					}
						
				}
				if(count > max)
				{
					max = count;
				}
			}
		}
		printf("%d\n",max);
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值