"spoj705" Distinct substrings

Source: Internet
Author: User

"Title description"

Given a string, calculate the number of its different substrings.

"Input Format"

A single string containing only uppercase letters, length <=50000

"Output Format"

A line is a positive integer, which is the number of different substrings.

"Sample Input"
Ababa
"Sample Output"
9

"Ideas"
I can see that it is a suffix array problem ~ But I will not write Qaq. Have to learn now use ~
At the end of the string to fill a ' $ ', not because of the other only because it is smaller than ' A '. Otherwise, you can make the ASCII code 0.
Apply rank array and SA array, rank[i]=j represents suffix i row j, sa[i]=j for rank i is suffix J. In other words, rank and SA are the opposite operations.
First, the SA array is ordered in alphabetical order, updating the rank array, but remember that the same rank should be the same, that is, if STR[SA[I]]==STR[SA[I-1]] Rank[sa[i]]=rank[sa[i-1]];else rank[ sa[i]]=rank[sa[i-1]]+1;
  
The k is then enumerated from 0, and each time the SA is continued to be sorted, but with Rank[sa[i]] as the first keyword and rank[sa[i]+2k] for the second keyword.
In Trank (in fact, Tmprank) update the new rank value, just as remember if RANK[SA[I]]==RANK[SA[I-1]]&AMP;&AMP;RANK[SA[I]+2K]==RANK[SA[I-1]+2K] The rankings do not rise.
Repeat this step until the 2k>=n or all suffixes are ranked differently.
Then normally k increases logn times, each time if with a count order as long as O (N), altogether O (Nlogn).
But don't write the count sort Ah qaq. So get in the queue. Add a log, generally will not be the card bar.
After calculating the SA and rank also calculate the height array, height[i] for the longest public prefix of sa[i] and sa[i-1], if the order of the 1--n is O (N2), it is obviously not good enough, so we calculate in a strange order.
First Count Height[rank[1]], then height[rank[2]] ...
That would have a nature: height[rank[i]]>=height[rank[i-1]]-1
I don't know why, but that's the way it is.
And then there's no more.
We know that any substring is a prefix of one suffix
For suffix I, there are length-i prefixes, where height[i] and the previous suffix are the same
So the answer is Σlength-i-height[i]

1#include <iostream>2#include <cstring>3#include <string>4#include <cstdio>5#include <cstdlib>6#include <cmath>7#include <algorithm>8#include <queue>9#include <stack>Ten#include <map> One#include <Set> A#include <list> -#include <vector> -#include <ctime> the#include <functional> - #definePRITNF printf - #defineScafn scanf - #defineSACNF scanf + #definefor (i,j,k) for (int i= (j); i<= (k);(i) + +) - #defineClear (a) memset (A,0,sizeof (a)) + using namespacestd; Atypedef unsignedintUint; at Const intinf=0x3fffffff; - Const Doubleeps=1e-Ten; - ///==============struct declaration============== -  - ///==============var declaration================= - Const intmaxn=50010; in intn,k; - intSA[MAXN],RANK[MAXN],H[MAXN],TRANK[MAXN],HEIGHT[MAXN]; to CharSTR[MAXN]; + ///==============function declaration============ - BOOLcmpintAintb) {returnRank[a]==rank[b]?rank[a+ (1<<k)]<rank[b+ (1&LT;&LT;K)]:rank[a]<rank[b];} the BOOLCMP1 (intAintb) {returnstr[a]<str[b];} * ///==============main code======================= $ intMain ()Panax Notoginseng { -scanf"%s", str+1); theN=strlen (str+1); str[n+1]='$'; n++; +      for(intI=0; i<=n;i++) Asa[i]=i; theSort (sa+1, sa+1+n,cmp1); +rank[sa[1]]=1; -      for(intI=2; i<=n;i++) $         if(str[sa[i]]!=str[sa[i-1]]) $rank[sa[i]]=rank[sa[i-1]]+1; -         Else -rank[sa[i]]=rank[sa[i-1]]; the      for(k=0;(1&LT;&LT;K) <=n;k++){ -Sort (sa+1, sa+1+n,cmp);Wuyitrank[sa[1]]=1; the          for(intI=2; i<=n;i++){ -             if(rank[sa[i]]!=rank[sa[i-1]]|| rank[sa[i]+ (1&LT;&LT;K)]!=rank[sa[i-1]+(1<<k)]) Wutrank[sa[i]]=trank[sa[i-1]]+1; -             Else Abouttrank[sa[i]]=trank[sa[i-1]]; $         } -          for(intI=1; i<=n;i++) -rank[i]=Trank[i]; -         if(rank[sa[n]]==n) Break; A     } +      for(intI=1; i<=n;i++) therank[sa[i]]=i; -     ///Height[i] The longest prefix for sa[rank[i] and Sa[rank[i-1]] $height[rank[1]]=0; the      for(intI=1; i<=n;i++){ theHeight[rank[i]]=max (height[rank[i-1]]-1,0); the         intp=i,q=sa[rank[i]-1]; the          while(str[p+height[rank[i]]]==str[q+Height[rank[i]]) -height[rank[i]]++; in     } the     Long Longans=0; the      for(intI=1; i<=n;i++) Aboutans+=n-sa[i]-Height[i]; theprintf"%lld\n", ans); the    return 0; the } + ///================fuction code====================
Spoj 705

As much as possible to do the title of the suffix Array, practice makes perfect, now just learned the suffix array, but also a lot of refueling is.

"spoj705" Distinct substrings

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.