codeforces 864B 之 Polycarp and Letters

Polycarp and Letters
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard 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
Note

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 78 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;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值