zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    FlatBuffers要点
    tarjan+缩点+强连通定理
    编程之美2.16 最长递增子序列
    Android Studio之多个Activity的滑动切换(二)
    Effective java读书札记第一条之 考虑用静态工厂方法取代构造器
    【PM】关于系统数据库和服务现场升级的一些看法
    用户及权限基础 2---- 权限
    Android双向滑动菜单完全解析,教你如何一分钟实现双向滑动特效
    【转贴】gdb中的信号(signal)相关调试技巧
    基于新浪sae使用php生成图片发布图文微博
  • 原文地址:https://www.cnblogs.com/wkfvawl/p/9472285.html
Copyright © 2011-2022 走看看