Hihocoder #1457: suffix automaton four • Repeat Melody 7 (suffix automaton + topological sort)

Source: Internet
Author: User

http://hihocoder.com/problemset/problem/1457

Val[i] Represents the sum of the decimal of all the strings represented by state I

Ans=∑val[i]
On the suffix automaton, any path from the starting state is reached in any state, and the character on this path is one of the strings that arrive at the state.

So use topological sorting to record the number of paths that go from the starting state to this state, that is, the number of strings in this state sum

If there is an edge u-->v on the suffix automaton, the digital m,sum[v]+=sum[u],val[v]+=val[u]*10+sum[u]*m is added.

As for multiple strings, the middle plus special characters, topological sort of time do not take the special character edge can

#include <cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespacestd;Const intmod=1e9+7;#defineN 2000001inttot=1,ch[n<<2][ One];intfa[n<<1],len[n<<1];intdep[n<<1];intlast=1, P,Q,NP,NQ;CharS[n];intv[n<<1];intsa[n<<1];intsum[n<<1],val[n<<1];voidExtendintc) {LEN[NP=++tot]=len[last]+1; DEP[NP]=dep[last]+1;  for(P=last;p &&!ch[p][c];p =fa[p]) ch[p][c]=NP; if(!p) fa[np]=1; Else{Q=Ch[p][c]; if(len[q]==len[p]+1) fa[np]=Q; Else{NQ=++tot; FA[NQ]=Fa[q]; memcpy (Ch[nq],ch[q],sizeof(Ch[nq])); FA[Q]=fa[np]=NQ; LEN[NQ]=len[p]+1; DEP[NQ]=dep[p]+1;  for(; ch[p][c]==q;p=fa[p]) ch[p][c]=NQ; }} last=NP;}voidADD (int&x,inty) {x+=y; X-=x>=mod? MoD:0;} intMain () {intN; scanf ("%d",&N); intL;  while(n--) {scanf ("%s", s+1); L=strlen (s+1);  for(intI=1; i<=l;++i) Extend (s[i]-'0'); if(n) Extend (Ten); }     for(intI=1; i<=tot;++i) v[dep[i]]++;  for(intI=1; i<=tot;++i) v[i]+=v[i-1];  for(intI=1; i<=tot;++i) sa[v[dep[i]]--]=i; sum[1]=1; intx;  for(intI=1; i<tot;++i) {x=Sa[i];  for(intj=0;j<Ten;++j)if(Ch[x][j]) {ADD (sum[ch[x][j]],sum[x]); ADD (Val[ch[x][j]), (1LL*val[x]*Ten+1LL*J*SUM[X])%MoD); }    }    intans=0;  for(intI=1; i<=tot;++i) ADD (Ans,val[i]); printf ("%d", ans);}

Time limit: 15000ms single point time limit: 3000ms memory limit: 512MB description

Small hi usually a big hobby is playing the piano. We know that a musical melody can be expressed as a series of numbers.

The magic is small hi found a name called "Decimal March Encyclopedia" of the portfolio, as the name implies, the collection has many works, but all the works have a common feature: only 10 notes, all the notes are represented as 0-9 of the number.

Now little hi wants to know the "sum" of all the different melodies in this work (that is, the string as a number, the sum in decimal, which allows for a leading 0). The answer is likely to be large, and we need to get a feel for (10^9 + 7).

Tips on how to solve problems

Input

The first line, an integer n, represents a work of N.

Next n rows, each row contains a string s consisting of the number 0-9.

All string lengths and no more than 1000000.

Output

A total of one line, an integer, that represents the answer mod (10^9 + 7).

Sample input
210109
Sample output
131

Hihocoder #1457: suffix automaton four • Repeat Melody 7 (suffix automaton + topological sort)

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.