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.
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.
Print maximum number of elements in pretty set of positions for string s.
11 aaaaBaabAbA
2
12 zACaAbbaazzC
3
3 ABC
0
In the first example the desired positions might be 6 and 8 or 7 and 8. Positions 6 and 7 contain letters 'a', position 8 contains letter 'b'. The pair of positions 1 and 8 is not suitable because there is an uppercase letter 'B' between these position.
In the second example desired positions can be 7, 8 and 11. There are other ways to choose pretty set consisting of three elements.
In the third example the given string s does not contain any lowercase letters, so the answer is 0.
题意:给定一个长度为n的字符串,求最大的位置集合,每个位置都是小写字母且各不相同,每两个位置之间没有大写字母
AC代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=200+10;
char a[maxn],s[maxn];
bool index[maxn];
bool islow(char c) //判断字符c是否为小写字母
{
if(c>='a' && c<='z')
return true;
return false;
}
bool isinc(char c,char a[],char n) //判断字符c是否在长度为n的数组中
{
for(int i=0;i<n;i++)
{
if(c==a[i])
return true;
}
return false;
}
int main()
{
int n;
while(cin>>n)
{
memset(s,0,sizeof(s));
memset(a,0,sizeof(a));
memset(index,false,sizeof(index));
scanf("%s",a);
int num=0;
int ans=-1;
for(int i=0;i<n;i++) //找出字符串中所有的小写字母有哪些
{
if(islow(a[i]) && !isinc(a[i],s,num))
s[num++]=a[i];
}
for(int i=0;i<n;i++)
{
memset(index,false,sizeof(index));
while(islow(a[i])) //判断当前的连续小写字母字符串
{
for(int j=0;j<num;j++)
{
if(s[j]==a[i])
{
index[j]=true;
break;
}
}
i++;
}
int flag=0;
for(int j=0;j<num;j++)//求出 当前的连续小写字母字符串对应的位置集合的元素个数
{
if(index[j]==true)
{
flag++;
}
}
ans=max(ans,flag);//更新答案
}
cout<<ans<<endl;
}
return 0;
}