HihoCoder_1445 _ suffix 2 & #183; repeat melody 5,

Source: Internet
Author: User

HihoCoder_1445 _ suffix 2. Repeated melody 5,
#1445: suffix 2. Repeated melody 5 time limit: Ms single point time limit: Ms memory limit: MB description

Xiao Hi's usual hobby is playing the piano. We know that a Music Melody is represented as a series of numbers.

Now, Xiao Hi wants to know how many different melodies appear in a piece of work?

Solution tips

Input

Contains a string consisting of lowercase letters. The length of a string cannot exceed 1000000.

Output

A row contains an integer indicating the answer.

Sample Input
aab
Sample output
5
  • In a suffix automatic machine, a node represents several consecutive suffixes.
  • Continuous means that the lengths of the suffixes ending with the same node are consecutive, for example, the lengths are 3, 4, and 5, respectively.
  • The shortest suffix length of the current node is the len + 1 that the par points to the node.
  • Therefore, the current node indicates that the number of all suffixes is the maximum length of the suffix ending with the current node minus the minimum length plus one
  • Generally, the minlen of the current node does not need to be stored in the suffix automatic machine. You only need to access the len of the par node. The len in the suffix automatic machine generally refers to maxlen, and both the par and link share the same meaning.

 

 

 1 #include <iostream> 2 #include <string> 3 #include <cstdio> 4 #include <cstring> 5 #include <algorithm> 6 #include <climits> 7 #include <cmath> 8 #include <vector> 9 #include <queue>10 #include <stack>11 #include <set>12 #include <map>13 using namespace std;14 typedef long long           LL ;15 typedef unsigned long long ULL ;16 const int    maxn = 1e6 + 10   ;17 const int    inf  = 0x3f3f3f3f ;18 const int    npos = -1         ;19 const int    mod  = 1e9 + 7    ;20 const int    mxx  = 100 + 5    ;21 const double eps  = 1e-6       ;22 const double PI   = acos(-1.0) ;23 24 struct SAM{25     int n, root, last, tot;26     struct node{27         int len;28         int link, go[26];29     };30     node state[maxn<<1];31     void init(char *str){32         n=strlen(str);33         tot=1;34         root=1;35         last=1;36         memset(&state,0,sizeof(state));37     }38     void extend(int w){39         tot++;40         int p=last;41         int np=tot;42         state[np].len=state[p].len+1;43         while(p && state[p].go[w]==0){44             state[p].go[w]=np;45             p=state[p].link;46         }47         if(p==0){48             state[np].link=root;49         }else{50             int q=state[p].go[w];51             if(state[p].len+1==state[q].len){52                 state[np].link=q;53             }else{54                 tot++;55                 int nq=tot;56                 state[nq].len=state[p].len+1;57                 memcpy(state[nq].go,state[q].go,sizeof(state[q].go));58                 state[nq].link=state[q].link;59                 state[q].link=nq;60                 state[np].link=nq;61                 while(p && state[p].go[w]==q){62                     state[p].go[w]=nq;63                     p=state[p].link;64                 }65             }66         }67         last=np;68     }69     void build(char *str){70         init(str);71         for(int i=0;i<n;i++)72             extend(str[i]-'a');73     }74     LL substring_num(void){75         LL ans=0LL;76         for(int i=root;i<=tot;i++)77             ans+=state[i].len-state[state[i].link].len;78         return ans;79     }80 };81 SAM A;82 char str[maxn];83 int main(){84     // freopen("in.txt","r",stdin);85     // freopen("out.txt","w",stdout);86     while(~scanf("%s",str)){87         A.build(str);88         printf("%lld\n",A.substring_num());89     }90     return 0;91 }

 

 

 

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.