Ultraviolet A 11572-unique snowflakes (slightly different from the method in the book)

Source: Internet
Author: User

Set is used in Liu lujia's book,

Using the set to search for. Count () and delete. Erase (). This method is better than mine and takes a shorter time.

I think map can also complete this task, but it is not convenient to delete it. You need to first find the find () subscript and then delete the element corresponding to this subscript.

However, for map usage, the following method is easier to implement.

I thought it would be faster to calculate ans after reading this side, but in fact it is better to read and then use set to process Explain faster.

#include<cstdio>#include<iostream>#include<map>#include<string>#include<algorithm>using namespace std;map<string,int> ma;int main(){    int t,n;    int ans=0;    scanf("%d",&t);    while(t--)    {        ans=0;        scanf("%d",&n);        ma.clear();        string s1;        int L=0,R=0;        for(int i=0;i<n;i++)        {            cin>>s1;            if(ma.count(s1)&&ma[s1]>=L)            {                L=ma[s1]+1;            }            R++;            ans=max(ans,R-L);            ma[s1]=i;        }        printf("%d\n",ans);    }    return 0;}


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.