• Polycarp and Letters(set首战!)


    Description

    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.

    Sample Input

    Input
    11
    aaaaBaabAbA
    Output
    2
    Input
    12
    zACaAbbaazzC
    Output
    3
    Input
    3
    ABC
    Output
    0

    Hint

    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.

    题目意思:求连续的小写子母中,种类最多的是多少种。

    解题思路:利用set中存储的元素的唯一 性去重,第一次使用set,还是对set中的删除不是很熟悉,当时使用set的迭代器也就是一个个删除的

    for(it=st.begin(); it!=st.end();)
         {
              st.erase(it++);
         }

    实际上set中含有直接清空容器的函数clear().

     1 #include<cstring>
     2 #include<cstdio>
     3 #include<algorithm>
     4 #include<set>
     5 using namespace std;
     6 char s[210];
     7 int main()
     8 {
     9     int n,i,sum,sum1;
    10     char a;
    11     sum=0;
    12     sum1=0;
    13     scanf("%d",&n);
    14     getchar();
    15     scanf("%s",s);
    16     set<char>st;
    17     set<char>::iterator it;
    18     for(i=0; i<n; i++)
    19     {
    20         if(s[i]>='A'&&s[i]<='Z')
    21         {
    22             sum1=st.size();
    23             /*for(it=st.begin(); it!=st.end();)
    24             {
    25                 st.erase(it++);
    26             }*/
    27             st.clear();
    28             if(sum1>sum)
    29             {
    30                 sum=sum1;
    31             }
    32         }
    33         else if(s[i]>='a'&&s[i]<='z')
    34         {
    35             st.insert(s[i]);
    36         }
    37     }
    38     sum1=st.size();
    39     if(sum1>sum)
    40     {
    41         sum=sum1;
    42     }
    43     printf("%d
    ",sum);
    44 }
  • 相关阅读:
    openresty开发系列12--lua介绍及常用数据类型简介
    openresty开发系列11--openresty的api入门
    SSD基本工作原理
    SSD 为什么顺序写比随机写性能更好?
    df看到的文件系统容量跟parted看到的分区容量差别较大的解决方法
    DPDK无法分出连续大页面(contiguous hugepages)的几个解决方法
    How to use, monitor, and disable transparent hugepages in Red Hat Enterprise Linux 6
    Examining Huge Pages or Transparent Huge Pages performance
    Notes of O_DIRECT flag
    leveldb
  • 原文地址:https://www.cnblogs.com/wkfvawl/p/9472285.html
Copyright © 2020-2023  润新知