Bzoj 2251: [2010Beijing Wc] Alien Contact suffix array

Source: Internet
Author: User

2251: [2010Beijing Wc] Alien contact time limit:30 Sec Memory limit:256 MB
submit:424 solved:232
[Submit] [Status] [Discuss] Description

Small P in the movie "Super-time contact" after being deeply moved, determined to focus on finding
Looking for an alien career. So he climbed on the roof every night trying to listen to the alien on his own radio.
Information sent by people. Although he received only a few noises, he still followed the high
The low level overwrites the received signal to a string of 0 and 1, and believes that the alien information is hidden in
which He believed that the information sent by aliens would be repeated in the 01 strings he received, so
He wanted to find all the substrings in the 01 strings that he had received that were more than 1 occurrences. But he got it.
The signal string is too long, so he wants you to make up a program to help him.

Input

The first line of the input file is an integer n, which represents the length of the signal string received by the small P.
The second line of the input file contains a 01-string length of N, which represents the signal string received by the small P.

Output

Each line of the output file contains a number of occurrences of a substring that appears more than 1. The output of the CIS
The order is arranged in the dictionary order of the corresponding substring.

Sample Input7
1010101Sample Output3
3
2
2
4
3
3
2
2
HINT

For 100% of data, meet 0 <=?? N <=3000

It is supposed to be written before the algorithm template problem should not be written again, but I have the suffix array is exactly the same as Xiang, or write again.

The suffix array needs to be twice times the size of the memory, I will not repeat this question. The main problem is to find the height array, before always feel the order problem is very annoying, in fact, it is not difficult, as long as to figure out the transfer order of height, the height of a position to take it in the "string position" in the previous position of the height value on the line, So the For statement should enumerate the position of the original array one time.

The rest is relatively simple, I use O (n^2) time complexity to deal with the answer, do not know if there is no faster, a little bit of attention, the dictionary ordering note the starting position of the same substring order, its order is the reverse of the enumeration order. In other words, we avoid falling into another hole when we are bypassing a pit.

Finally, HBW mentions a method of optimizing the random string suffix array to O (n), and when the rank array has a maximum value of n, it simply break off, seemingly a simple and useful optimization

#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<vector>using namespacestd;#defineMAXN 3010*2CharSTR[MAXN];intSA[MAXN],TSA[MAXN];intRANK[MAXN],TRANK[MAXN];intBUC[MAXN];intHEIGHT[MAXN];intTHEIGHT[MAXN];voidIndexsort (intJpintN) {memset (Buc,0,sizeof(BUC));  for(intI=0; i<n;i++) buc[rank[i+jp]]++;  for(intI=1; i<=n;i++) buc[i]+=buc[i-1];  for(inti=n-1; i>=0; i--) tsa[--buc[rank[i+jp]]]=i; memset (Buc,0,sizeof(BUC));  for(intI=0; i<n;i++) buc[rank[tsa[i]]]++;  for(intI=1; i<=n;i++) buc[i]+=buc[i-1];  for(inti=n-1; i>=0; i--) sa[--buc[rank[tsa[i]]]]=tsa[i];}voidSuffixarray (Char* STR,intN) {         for(intI=0; i<n;i++) trank[i]=str[i]-'0'+1;  for(intI=0; i<n;i++) buc[trank[i]]++;  for(intI=1; i<=n;i++) buc[i]+=buc[i-1];  for(inti=n-1; i>=0; i--) sa[--buc[trank[i]]]=i;  for(intI=0, x=0; i<n;i++)        {                if(!i | | trank[sa[i]]!=trank[sa[i-1]]) x + +; Rank[sa[i]]=x; }         for(intj=1;j<n;j=j<<1) {indexsort (j,n); intx=0;  for(intI=0; i<n;i++)                {                        if(!i | | rank[sa[i]]!=rank[sa[i-1]] || rank[sa[i]+j]!=rank[sa[i-1]+J]) x + +; Trank[sa[i]]=x; }                 for(intI=0; i<n;i++) rank[i]=Trank[i]; if(x==n) Break; }}voidInitheight (intN) {         for(intI=0; i<n;i++)        {                if(rank[i]==1)Continue; Height[i]=max (height[i-1]-1,0);  while(I+height[i]<n && sa[rank[i]-2]+height[i]<N&& str[i+height[i]]==str[sa[rank[i]-2]+Height[i]]) Height[i]++; }         for(intI=1; i<n;i++) Theight[i]=Height[sa[i]];} Vector<int>Vec;intstack[maxn],tops=-1;intMain () {Freopen ("Input.txt","R", stdin); intN; intx; scanf ("%d\n",&N); scanf ("%s\n", str);        Suffixarray (Str,n);    Initheight (n); //for (int i=0;i<n;i++) printf ("%d", Sa[i]);p rintf ("\ n"); //for (int i=0;i<n;i++) printf ("%s\n", Str+sa[i]);p rintf ("\ n"); //for (int i=0;i<n;i++) printf ("%d", Height[i]);p rintf ("\ n");         for(intI=1; i<n;i++)        {                if(theight[i]<=theight[i-1])Continue; X=i;  for(intk=theight[i];k>theight[i-1];k--)                {                         while(x+1<n && theight[x+1]&GT;=K) x + +; stack[++tops]=x-i+2; }                 while(~tops) printf ("%d\n", stack[tops--]); }}

Bzoj 2251: [2010Beijing Wc] Alien Contact suffix array

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.