"Topic link"
http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=51267
Test instructions
Given n strings, computes the sum of all non-heavy integers formed by substrings that omit leading 0.
Ideas
Since we are dealing with substring problems, we can construct a SAM after merging the strings.
Sam's nature: the set of strings represented by the node U is the sum of the set of strings represented by all the nodes on the root to the U path, and the set of strings represented by the node U is the longest string of all substrings.
Cnt[u] is the number of strings represented by the state U, and Sum[u] represents the sum of the corresponding number of strings represented by the state U.
After the topology is sorted, p is the current node, and trans (P,J) is NP, then:
Cnt[np]+=cnt[p]
Sum[np]+=sum[p]*10+cnt[p]*j
Do not walk $ side, the first time not to walk 0 sides.
Code
1#include <Set>2#include <cmath>3#include <queue>4#include <vector>5#include <cstdio>6#include <cstring>7#include <iostream>8#include <algorithm>9 #defineTrav (u,i) for (int i=front[u];i;i=e[i].nxt)Ten #definefor (A,B,C) for (int a= (b); a<= (c); a++) One using namespacestd; A -typedefLong Longll; - Const intN = 2e5+Ten; the Const intMOD = -; - - intN,len; - CharS[n]; + intSum[n],cnt[n]; - intsz,last,fa[n],ch[n][ One],l[n],c[n],b[n]; + A voidAddintc) at { - intP=last,np=++sz; last=NP; -l[np]=l[p]+1; - for(;p &&!ch[p][c];p =fa[p]) ch[p][c]=NP; - if(!p) fa[np]=1; - Else { in intq=Ch[p][c]; - if(l[q]==l[p]+1) fa[np]=Q; to Else { + intNq=++sz; l[nq]=l[p]+1; -memcpy (Ch[nq],ch[q],sizeof(Ch[q])); thefa[nq]=Fa[q]; *fa[q]=fa[np]=NQ; $ for(; ch[p][c]==q;p=fa[p]) ch[p][c]=NQ;Panax Notoginseng } - } the } + voidInit () A { thesz=last=1; +len=0; -memset (SUM,0,sizeof(sum)); $memset (CNT,0,sizeof(CNT)); $memset (CH,0,sizeof(CH)); -Memset (c,0,sizeof(c)); -memset (FA,0,sizeof(FA)); thememset (L,0,sizeof(l)); - }Wuyi intMain () the { - while(SCANF ("%d", &n) = =1) Wu { - init (); Aboutfor (I,1, N) { $scanf"%s", s); - if(i!=1) Add (Ten), len++; - for(intj=0; s[j];j++) -Add (s[j]-'0'), len++; A } +for (I,1, SZ) c[l[i]]++; thefor (I,1, Len) c[i]+=c[i-1]; -for (I,1, SZ) b[c[l[i]]--]=i; $ the intans=0; thecnt[1]=1; thefor (I,1, SZ) { the intp=B[i]; - for(intj=0;j<Ten; j + +) { in if(i==1&&j==0)Continue; the if(Ch[p][j]) { the intnp=Ch[p][j]; AboutCnt[np]= (Cnt[np]+cnt[p])%MOD; theSum[np]= (sum[np]+sum[p]*Ten+CNT[P]*J)%MOD; the } the } +Ans= (Ans+sum[p])%MOD; - } theprintf"%d\n", ans);Bayi } the return 0; the}
UVA1673 Str2Int (SAM)