Digital string (Codevs 1394)

Source: Internet
Author: User

Title Description Description

Give you a string of numbers of length n, which will contain 1-m numbers. If a contiguous number of substrings contains 1-m these numbers, the number string is said to be num strings. Your task is to find out what the shortest num string is, just output this length.
1<=n,m<=200000

Enter a description Input Description

The first line is given N and M.
The second row n number, representing the number string, between the numbers separated by a space.

Output description Output Description

If the NUM string is present, output the shortest num string length, otherwise output "NO".

Sample input Sample Input

5 3
1 2 2) 3 1

Sample output Sample Output

3

Data range and Tips Data Size & Hint

Each test point 1s

/*set the L and R sequence of the left and right end points, the length is r-l+1, first increase R, and count the number of times each number appears, 1~m all together, and then increase the L, when a number appears more than 1 times, you can abandon it until a certain number appears once, it must be the edge of the loop to judge, Because in the process of the cycle, there is constant to meet in the 1~m in the number of add in, so the optimal solution is constantly updated. */#include<cstdio>#include<iostream>#defineM 200010#defineINF 0x3f3f3f3fusing namespacestd;intA[m],sum[m];intMain () {intn,m; scanf ("%d%d",&n,&m);  for(intI=1; i<=n;i++) scanf ("%d",&A[i]); intL=1, cnt=0, ans=INF;  for(intR=1; r<=n;r++)    {        if(a[r]<1|| A[R]&GT;M)Continue; if(!sum[a[r]]) cnt++; SUM[A[R]]++; if(cnt==m) { for(inti=l;i<=n;i++)            {                if(sum[a[i]]>1) {Sum[a[i]]--; L++; }                Else  Break; } ans=min (ans,r-l+1); }    }    if(cnt!=m) printf ("NO"); Elseprintf"%d", ans); return 0;}
View Code

Digital string (Codevs 1394)

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.