"Spoj" 8222. substrings (suffix automaton)

Source: Internet
Author: User

http://www.spoj.com/problems/NSUBSTR/

Test instructions: Gives a string s, so that f (x) represents the maximum number of occurrences of all the lengths of X in a substring of s. Ask F (1). F (Length (S))

This approach:

First, set up a suffix automaton for the string.

Because each state in the automaton represents a class of substring prefixes, and the substring corresponding to the longest |max| of any state is unique.

So we work out each substring (that is, the found state is the end state), their right value is + + (that is, the number of occurrences is guaranteed to be 1 for the current substring), then the right value is updated from the bottom up in the parent tree (the right value in the parent tree is the number of occurrences, So include the subtree to add the subtree right).

Then you can get the right value for all lengths. The right value is the number of occurrences of the substring.

You can now update the number of occurrences with a length of |max| for each state length.

Because we find only Max, but because: there must be so many short strings in the firstborn string (ancestor in the parent tree), you can update the short string with a long string.

Update the right value to be updated from a long length (because the length is from long to short, corresponds to the relationship between the leaf node and the inner node of the parent tree, the number of the right values of the state is monotonically unchanged), so we first row, then from the big to the small update.

This will find out the number of occurrences of all substrings.

#include <cstdio> #include <cstring> #include <cmath> #include <string> #include <iostream > #include <algorithm> #include <queue> #include <set> #include <map>using namespace std; typedef long Long LL; #define REP (i, n) for (int i=0; i< (n); ++i) #define FOR1 (i,a,n) for (int i= (a); i<= (n); ++i) #define For2 (i,a,n) for (int i= (a);i< (n), ++i) #define FOR3 (i,a,n) for (int i= (a); i>= (n); i.) #define FOR4 (i,a,n) for (int i= ( a);i> (n); i) #define CC (i,a) memset (i,a,sizeof (i)) #define READ (a) a=getint () #define PRINT (a) printf ("%d", a) # Define DBG (x) cout << (#x) << "=" << (x) << endl#define error (x) (! x) puts ("error"): 0) #define RDM (x, i) for (int i=ihead[x]; i; i=e[i].next) inline const int Getint () {int r=0, k=1; Char c=g Etchar (); for (; c< ' 0 ' | | C> ' 9 '; C=getchar ()) if (c== '-') k=-1; for (; c>= ' 0 ' &&c<= ' 9 '; C=getchar ()) r=r*10+c-' 0 '; return k*r; }struct sam {static const int n=1000005;int c[n][26], l[n], F[n],Root, Last, Cnt;sam () {cnt=0; root=last=++cnt;} void Add (int x) {int now=last, a=++cnt; last=a;l[a]=l[now]+1;for (; now &&!c[now][x]; Now=f[now]) c[now][x]=a;if ( !now) {f[a]=root; return;} int Q=c[now][x];if (l[q]==l[now]+1) {f[a]=q; return;} int b=++cnt;memcpy (c[b], c[q], sizeof c[q]); L[b]=l[now]+1;f[b]=f[q];f[q]=f[a]=b;for (; now && c[now][x]==q; now =f[now]) c[now][x]=b;} void Build (char *s) {int Len=strlen (s); Rep (I, Len) Add (s[i]-' a ');}} a;const int N=250005;char s[n];int f[n], r[1000005], t[n], b[1000005];void getans (int len) {int *l=a.l, *P=A.F, cnt=a.cnt; for (int now=a.root, i=0; i<len; ++i) now=a.c[now][s[i]-' A '], ++r[now];for1 (i, 1, CNT) ++t[l[i]];for1 (i, 1, len) t[i]+=t [I-1];for1 (I, 1, CNT) B[t[l[i]]--]=i;for3 (i, CNT, 1) r[p[b[i]]]+=r[b[i]];for1 (i, 1, CNT) F[l[i]]=max (F[l[i]], r[i]); For3 (I, Len, 1) F[i]=max (f[i+1], f[i]), For1 (i, 1, len) printf ("%d\n", F[i]);} int main () {scanf ("%s", s); A.build (s); Getans (strlen (s)); return 0;}

  

8222. Substringsproblem Code:nsubstr

You is given a string S which consists of 250000 lowercase Latin letters at the most. We define F (x) as the maximal number of times that some string with length x appears in S. For example for string ' Ababa ' F (3) 'll be 2 because there is a string ' aba ' that occurs twice. Your task is to output F (i) for every i so that 1<=i<=| s|.

Input

String S consists of the most 250000 lowercase latin letters.

Output

Output | s| Lines. On the i-th line output F (i).

Example
Input:
Ababa

Output:
3
2
2
1
1

"Spoj" 8222. substrings (suffix automaton)

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.