B. Polycarp and Letters 题解(人生第一篇博客,写的不好请多指教)

B. Polycarp and Letters

题目出处:http://codeforces.com/problemset/problem/864/B

  • time limit per test2 seconds
  • memory limit per test256 megabytes
  • inputstandard input
  • outputstandard output

Polycarp loves lowercase letters and dislikes uppercase ones. Once he got a string s consisting only of lowercase and uppercase Latin letters.

Let A be a set of positions in the string. Let’s call it pretty if following conditions are met:

letters on positions from A in the string are all distinct and lowercase;
there are no uppercase letters in the string which are situated between positions from A (i.e. there is no such j that s[j] is an uppercase letter, and a1 < j < a2 for some a1 and a2 from A).
Write a program that will determine the maximum number of elements in a pretty set of positions.

Input
The first line contains a single integer n (1 ≤ n ≤ 200) — length of string s.

The second line contains a string s consisting of lowercase and uppercase Latin >letters.

Output
Print maximum number of elements in pretty set of positions for string s.
Examples
input
11
aaaaBaabAbA

output
2
input
12
zACaAbbaazzC

output
3
input
3
ABC

output
0

题解

这一题主要是统计连续的小写字母中,不同字母的个数,然后输出个数的最大值,就可以了。
哈哈哈题目看似复杂,实际上还是很简单的!

题目思路

定义一个char型的数组a[220],储存输入的字母,
然后声明了一个is_big()函数判断大写,
int型的数组abc[27],用来统计字母a到z的个数,
下面就是判断连续小写字母,和统计不同字母的个数啦~超级简单!

下面奉献上我的AC代码

    #include<stdio.h>
    #include<string.h>
    int is_big(char a)
    {
    	if(a>='A'&&a<='Z')
    		return 1;
    	else
    		return 0;
    }
    int main()
    {
    	int n;
    	while(scanf("%d",&n)!=EOF)
    	{
    		getchar();
    		char a[220];
    		int max=0,sum=0,abc[27];
    		for(int i=0;i<n;i++)
    			scanf("%c",&a[i]);
    		for(int i=0;i<n;i++)
    		{
    			memset(abc,0,sizeof(abc));
    			if(!is_big(a[i]))
    			{
    				abc[a[i] - 'a' ] = 1;
    				sum = 1;
    				for(int j=i+1;j<n;j++)
    				{
    					if(!(is_big(a[j])) && abc[a[j] - 'a'] != 1)
    					{
    						sum++;
    						abc[a[j] - 'a'] = 1;
    					}
    					else if(is_big(a[j]))
    						break;
    				}
    				if(sum>max)
    					max = sum;
    			}
    		}
    		printf("%d\n",max);
    	}
    	return 0;
     } 

题目的坑

要注意用getchar();吸收打完数字的换行符哦~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值