String (Multi-string suffix automaton): HDU 4436 Str2Int

Source: Internet
Author: User

Str2Int

Time limit:6000/3000 MS (java/others) Memory limit:131072/131072 K (java/others)
Total submission (s): 2082 Accepted Submission (s): 744


Problem DescriptionIn This problem, you is given several strings that contain only digits from ' 0 ' to ' 9 ', inclusive.
An example is shown below.
101
123
The set S of strings are consists of the N strings given in the input file, and all the possible substrings of them.
It ' s boring to manipulate strings, so you decide to convert strings in s into integers.
You can convert a string of contains only digits to a decimal integer, for example, you can convert "101" to 101, "0 1 "into 1, et al.
If An integer occurs multiple times, you are only keep one of them.
For example, in the example shown above, all the integers is 1, 10, 101, 2, 3, 12, 23, 123.
Your task is to calculate the remainder of the sum of all the integers you get divided by 2012.

Inputthere is no more than test cases.
The test case is starts by a line contains an positive integer N.
Next N lines each contains a string consists of one or more digits.
It ' s guaranteed that 1≤n≤10000 and the sum of the length of the strings≤100000.
The input is terminated by EOF.

Outputan integer between 0 and, inclusive, for each test case.

Sample Input510112309000 1234567890 Sample Output202 This problem with a suffix automaton can be well solved.
1#include <iostream>2#include <cstring>3#include <cstdio>4 using namespacestd;5 Const intMod= -;6 Const intmaxn=200010;7 intCNT,LAST,N,ANS,SUM[MAXN];8 intWV[MAXN],SA[MAXN],TOT[MAXN];9 intch[maxn][ A],FA[MAXN],LEN[MAXN];Ten structsam{ One     voidInit () { Amemset (CH,0,sizeof(CH)); -memset (FA,0,sizeof(FA)); -memset (Len,0,sizeof(len)); theLast=cnt=1; -     } -     voidInsert (intc) { -         intp=last,np=last=++cnt;len[np]=len[p]+1; +          while(P&&!ch[p][c]) ch[p][c]=np,p=Fa[p]; -         if(!p) fa[np]=1; +         Else{ A             intq=Ch[p][c]; at             if(len[p]+1==Len[q]) -fa[np]=Q; -             Else{ -                 intnq=++cnt;len[nq]=len[p]+1; -memcpy (Ch[nq],ch[q],sizeof(Ch[q])); -fa[nq]=fa[q];fa[q]=fa[np]=NQ; in                  while(p&&ch[p][c]==q) -ch[p][c]=nq,p=Fa[p]; to             }     +         } -     } the      *     voidExtend (Char*s) { $         intL=strlen (s); last=1;Panax Notoginseng          for(intI=0, c;i<l;i++){ -c=s[i]-'0'; the             if(!ch[last][c]| | len[ch[last][c]]!=i+1) + Insert (c); A             Else thelast=Ch[last][c]; +         } -     } $ }sam; $  - CharS[MAXN]; -  the intMain () { -      while(SCANF ("%d", &n)! =EOF) {Wuyi Sam. Init (); the          for(intI=1; i<=n;i++) -scanf"%s", s), Sam. Extend (s); Wu          -memset (WV,0,sizeof(WV)); Aboutmemset (SA,0,sizeof(SA)); $          for(intI=1; i<=cnt;i++) -wv[len[i]]++; -          for(intI=1; i<=cnt;i++) -wv[i]+=wv[i-1]; A          for(inti=cnt;i>=1; i--) +sa[--wv[len[i]]]=i; thememset (Tot,0,sizeof(tot)); -memset (SUM,0,sizeof(sum)); $tot[1]=1; ans=0;  the          for(intI=0; i<cnt;i++){ the             intx=Sa[i]; the              for(intj=0; j<=9; j + +){ the                 if(x==1&&j==0)Continue; -(Tot[ch[x][j]]+=tot[x])%=MoD; in(Sum[ch[x][j]]+= (sum[x]*Ten+TOT[X]*J))%=MoD; the             } the(Ans+=sum[x])%=MoD; About         } theprintf"%d\n", ans);  the     } the}

String (Multi-string suffix automaton): HDU 4436 Str2Int

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.