"Suffix automaton" "topology Sort" "Dynamic planning" hihocoder1457 suffix automaton four • Repeat Melody 7

Source: Internet
Author: User

Tips on how to solve problems

Little hi: We have learned the suffix automaton, and today we will look at this interesting topic.

Little ho: Good! This topic allows us to ask for a number of numbers and strings of all the different substrings.

Little hi: Can you combine the nature of the suffix automata to think about how to solve the problem?

Small ho: Since this topic is about substrings, then I know that the set of substrings contained in all States of the suffix automaton corresponds exactly to all the distinct substrings of the original string.

Little hi: Very good. So you can simplify the problem first, think of only a string how to do?

Little ho: OK. This is hard for me. The last time I knew how to count the numbers of all the different substrings in a string, the problem is similar, but the calculations are a little more complicated.

Little hi: Then you can tell me in detail.

Small ho: Let's take an example, assuming that s= "1122124" is actually the familiar example of "aabbabd".

Status Sub-string Endpos sum
S Empty string 0
1 1 {1,2,5} 1
2 11 {2} 11
3 112 {3} 112
4 1122,122,22 {4} 1266
5 2 {3,4,6} 2
6 11221,1221,221,21 {5} 12684
7 112212,12212,2212,212 {6} 126848
8 12 {3,6} 12
9 1122124,122124,22124,2124,124,24,4 {7} 1248648

Small ho: If we can find the "sum" of the substrings contained in each State as in the table above, it may be remembered as sum (ST). Then we only ask that Σsum (ST) Be the answer.

Little hi: So how do you find out what each state is and what it's about?

Small ho: Starting from the initial state of a hand-launched. For example, we now require that State 6 is the and of {11221,1221,221,21}. We know that the side (transition) of the arrival state 6 has 2, respectively trans[4][1] and trans[5][1]. If we have calculated sum (4) = 1266, sum (5) = 2, then we can find sum (6) = (SUM (4) * + 1 * |substrings (4) |]) + (Sun (5) * + 1 * |substring (5) |) = (12660 + 1 * 3) + (2 * 10 + 1 * 1) = 12684.

Small ho: In other words, the three substrings of {11221, 1221, 221} In state 6 are obtained from all (4) substrings of State 3 multiplied by 10 plus 1, and the {6} substring in state 21 is obtained from all (5) substrings of State 1 multiplied by 10 plus 1. That is to say, for the State St

Sum (ST) =σ{sum (x) * ten + c * |substrings (x) | | | trans[x][c] = st}.

Small ho: We know that Sam's state and transfer constitute a direction-free graph, we only ask for the topological order of the state, then we can find sum (ST) in turn.

Little hi: Good. Then we go back to the original question of multiple strings of the situation, how to solve?

Little ho: Multiple strings I won't be there. ┑ ( ̄д ̄) ┍

Little hi: Remember the technique we used to find the longest common string of multiple strings in a suffix array in the 122th week?

Little ho: To concatenate multiple strings with ' # ' as a string?

Little hi: Yes. This time we also use this method to put all strings with a colon ': ' (': ' Acii code is 58, that is, ' 0 ' ASCII code +10, easy to handle) since the connection. Take two strings "12" and "234" for example, "12:234" of Sam

status substring endpos | valid-substrings| sum
S empty string   1 0
1 1 {1} 1 1
2 {2} 1
3 12:,2 :,: {3} 0 0
4 12:2,2:2,:2 {4} 0 0
5 2 {2,4} 1 2
6 12:23,2:23,:23,23,3 {5} 2
7 12:234,2:234,:234,234,34,4 {6} 3 272

Little ho: It seems that if we exclude the substrings with colons in each state, it seems to be recursive!

Little hi: Yes. If we use Valid-substrings (ST) to represent all the substrings in a state without colons, we have a similar recursive type for sum (ST)

Sum (ST) =σ{sum (x) * ten + c * |valid-substrings (x) | | trans[x][c] = ST}

Small ho: So the key is |valid-substrings (ST) | How did you find out?

Little hi: Yes. |valid-substrings (ST) | Represents the number of substrings in the St without a colon, which is exactly the amount of all paths from the initial state s to the state St for all "sides without a colon".

Little ho: It seems a bit around.

Small hi: For example, for state 6, if we do not go through a transfer labeled ': ', then there are 2 paths from S to 61, which are s->6 and s->5->6, respectively, corresponding to substrings 3 and 23 without colons. As mentioned earlier, the state and transfer of Sam constitute a direction-free graph, and the number of paths on a non-circular graph is also a classic topological ordering problem, which can be consulted before our discussion

Little ho: I get it. After Sam has been built, sort all of the state topologies and then push the topological order recursively to find out the sum (ST) on the |valid-substrings (ST) side. Okay, I'm going to write the program.

#include <cstdio> #include <cstring> #include <queue>using namespace std;typedef long long ll; #define MOD 1000000007ll#define MAXL 2000000#define MAXC 11char s[maxl+10];int len/* text string length */;struct sam{int n/* status number 0~n-1*/,maxlen [2*maxl+10],minlen[2*maxl+10],trans[2*maxl+10][maxc],slink[2*maxl+10];int new_state (int _maxlen,int _minlen,int _ Trans[],int _slink) {maxlen[n]=_maxlen;minlen[n]=_minlen;for (int i=0;i<maxc;++i) {if (_trans==null) {trans[n][i]= -1;} Else{trans[n][i]=_trans[i];}} Slink[n]=_slink;return n++;} int Add_char (char ch,int u) {if (u==-1) {return new_state (0,0,null,-1);} int c=ch-' 0 '; int z=new_state (maxlen[u]+1,-1,null,-1); int v=u;while (v!=-1 && trans[v][c]==-1) {trans[v][c]=z; V=SLINK[V];} if (v==-1) {//The simplest case, Suffix-path (u->s) does not have a corresponding character Ch's transfer Minlen[z]=1;slink[z]=0;return Z;} int x=trans[v][c];if (maxlen[v]+1==maxlen[x]) {//simpler case without splitting Xminlen[z]=maxlen[x]+1;slink[z]=x;return Z;} int y=new_state (maxlen[v]+1,-1,trans[x],slink[x]);//The most complex case, split xslink[y]=slink[x];minlen[x]=maxlen[Y]+1;slink[x]=y;minlen[z]=maxlen[y]+1;slink[z]=y;int w=v;while (w!=-1 && trans[w][c]==x) {trans[w][c]=y;w= SLINK[W];} Minlen[y]=maxlen[slink[y]]+1;return Z;}} Sam;int m;queue<int>q;int ru[maxl*2+10];ll paths[maxl*2+10],f[maxl*2+10],ans;int Main () {//freopen (" Hihocoder1457.in "," R ", stdin), scanf ("%d ", &m), for (int i=1;i<=m;++i) {scanf ("%s ", S+len); Len=strlen (s); S[len] = ': '; s[++len]= ';} int U=sam.add_char (0,-1); for (int i=0;i<len;++i) {U=sam.add_char (s[i],u);} for (int i=0;i<sam.n;++i) {for (int j=0;j<maxc;++j) {if (sam.trans[i][j]!=-1) {++ru[sam.trans[i][j]];}}} paths[0]=1;for (int i=0;i<sam.n;++i) {if (!ru[i]) {Q.push (i);}} while (!q.empty ()) {U=q.front (), Q.pop (), for (int i=0;i<maxc;++i) {if (i!= ': '-' 0 ') {paths[sam.trans[u][i]]+=paths[u ];f[sam.trans[u][i]]= (f[sam.trans[u][i]]+ (f[u]*10ll%mod+ (LL) i* (paths[u]%mod)%mod)%mod)%MOD;} --ru[sam.trans[u][i]];if (!ru[sam.trans[u][i]]) {Q.push (sam.trans[u][i]);}} for (int i=0;i<sam.n;++i) {ans= (ans+f[i])%mod;} printf ("%lld\n", ans); return 0;}

Suffix automaton, topology sort, dynamic plan, hihocoder1457 suffix automaton four • Repeat Melody 7

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.