HDU 2668 Daydream (longest non-repeating subsequence)

Source: Internet
Author: User

Title Description:
Daydream
Time limit:1000ms Memory limit:32768kb 64bit IO format:%i64d &%i64u

Description
Welcome to HDU Girl's Cup, bless you'll happy in it.
Every girl is beautiful if you use the heart to feel. Every corner in the world would colourful and energetic by several girls standing. If you boy, a normal bay, I believe so you'll try to watch when a beautiful girl passing and you'll nervous if a Girl watching you just is watching her.

Now give you a surprise, May is never happy in the real time. Millions of PLMM stand in a line and facing you (^_^). They is dress colourful clothings. You should to find out a maximal subline PLMM that their clothing color is all different.

Input
The input contains multiple test cases.
Each case first give a integer n expressing many many girls stand on line. (n<=10000000)
Next Line a string including n character, each character standing one girls ' s clothing color.

Output
Output One integer The numbers of maximal subline PLMM that their clothing color is all different and the line's begin an D End (based from 0). If Their is more answer, print the begin smallest.

Sample Input

3
Abc
5
Aaaba
8
Hdugirls

Sample Output

3 0 2
2 2 3
8 0 7

Main topic:
is to ask you the length of the longest consecutive non-repeating subsequence in the given sequence, as well as the starting position, if there are multiple, output the first set of

Topic Analysis:
First look at the data size, 10^7, at least to get O (n) time complexity, first of all, the topic is said to be a character, but is not said to be lowercase letters, although the sample is lowercase, so the array open large point, 150 or 300 are OK, then said the idea, the sense of thinking is the ruler method, two pointers, One is set at the beginning, the other begins to sweep forward, and the position of the appearing letter exists in the array that was just opened, until the repetition occurs (the position of the array has been missed) then the length of the current scan sequence and the starting position are compared with the original, leaving a long sequence, Then move the back pointer to the next character in the position of the repeating character. It is important to note that we do not need to re-assign the array once each time we scan the longest sequence, just to see if the duplicate character is on the right side of the current tail pointer (the left side is not the same as the current scan).

Code:

#include "Cstdio"#include "CString"int POS[ Max];intMain () {intn,i,l,r,ltmp,cnt,cnttmp; Char ch; while(SCANF ("%d", &n)!=eof) {GetChar ();        Ch=getchar (); cnt=ltmp=0; cnttmp=1; MemsetPOS,-1, sizeof (POS));POS[ch]=0; for(i=1; i<n;i++) {Ch=getchar ();//Duplicate appearsif(POS[ch]>=0&&POS[ch]>=ltmp) {if(cnttmp>cnt)                    {cnt=cnttmp;                    l=ltmp; r=i-1; } cnttmp-=POS[Ch]-ltmp; ltmp=POS[ch]+1; }Else{cnttmp++; }//No matter the weight is not heavy, all the newest position is put over//because only the new position will affect the future, the old position must be in the end of the pointer before, uselessPOS[Ch]=i; }if(cnttmp>cnt)            {cnt=cnttmp;            l=ltmp; r=i-1; }printf("%d  %d%d \ n", cnt,l,r); }return 0;}

HDU 2668 Daydream (longest non-repeating subsequence)

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.