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();吸收打完数字的换行符哦~