AC Automata/Digital DP
Orz ZYF
Good question = = at the same time deepened my on AC automata (this should be called Trie map ...) Out side complement! ) and the digital DP understanding ... But it's really weak to write it by myself ...
1 /**************************************************************2 problem:35303 User:tunix4 language:c++5 result:accepted6 time:1008 Ms7 memory:33956 KB8 ****************************************************************/9 Ten //Bzoj 3530 One#include <vector> A#include <cstdio> -#include <cstring> -#include <cstdlib> the#include <iostream> -#include <algorithm> - #defineRep (i,n) for (int i=0;i<n;++i) - #defineF (i,j,n) for (int i=j;i<=n;++i) + #defineD (i,j,n) for (int i=j;i>=n;--i) - #definePB Push_back + using namespacestd; AInlineintGetint () { at intv=0, sign=1;CharCh=GetChar (); - while(ch<'0'|| Ch>'9'){if(ch=='-') sign=-1; Ch=GetChar ();} - while(ch>='0'&&ch<='9') {v=v*Ten+ch-'0'; Ch=GetChar ();} - returnv*Sign ; - } - Const intn= -, inf=~0u>>2, mod=1e9+7; intypedefLong LongLL; - /******************tamplate*********************/ to intN,m,cnt=1, a[n]; + structtrie{ - intch[Ten],fail; the BOOLSign ; *}t[n*Ten]; $ CharS[n],s1[n];Panax Notoginseng voidins () { -scanf"%s", S1); the intx=1, y; + Rep (I,strlen (S1)) { Ay=s1[i]-'0'; the if(! T[x].ch[y]) t[x].ch[y]=++CNT; +x=T[x].ch[y]; - } $t[x].sign=1; $ } - intQ[n]; - voidMake_fail () { the intL=0, r=-1, J; -q[++r]=1;Wuyi while(l<=R) { the intx=q[l++],y; -t[x].sign|=t[t[x].fail].sign; WuRep (I,Ten){ -j=T[x].fail; About while(J && t[j].ch[i]==0) j=T[j].fail; $ if(T[x].ch[i]) { -y=T[x].ch[i]; -T[y].fail=j? T[j].ch[i]:1; -q[++r]=y; A}ElseT[x].ch[i]=j? T[j].ch[i]:1; + } the } - } $ intf[n][n][2]; the intMain () { the #ifndef Online_judge theFreopen ("3530.in","R", stdin); theFreopen ("3530.out","W", stdout); - #endif inscanf"%s", s); then=strlen (s); theF (I,1, N) a[i]=s[i-1]-'0'; AboutM=getint (); Cnt=1; theF (I,0,9) t[1].ch[i]=++CNT; theF (I,1, m) ins (); the Make_fail (); + -F (I,1, a[1]) the if(! t[t[1].ch[i]].sign) f[1][t[1].ch[i]][i==a[1]]=1;BayiF (I,1, N-1) F (J,1, CNT) { theF (k,0, a[i+1])if(!t[t[j].ch[k]].sign) the(f[i+1][t[j].ch[k]][k==a[i+1]]+=f[i][j][1])%=MOD; -Rep (K,Ten)if(!t[t[j].ch[k]].sign) -(f[i+1][t[j].ch[k]][0]+=f[i][j][0])%=MOD; the } the intans=0; theF (I,1, CNT) (ans+=f[n][i][0])%=mod, (ans+=f[n][i][1])%=MOD; the -Memset (F,0,sizeoff); theF (I,1,9)if(! t[t[1].ch[i]].sign) f[1][t[1].ch[i]][0]=1; theF (I,1, N-2) F (J,1, CNT) theRep (K,Ten)if(!t[t[j].ch[k]].sign)94(f[i+1][t[j].ch[k]][0]+=f[i][j][0])%=MOD; theF (I,1, N-1) F (J,1, CNT) the(ans+=f[i][j][0])%=MOD; theprintf"%d\n", ans);98 return 0; About}View Code 3530: [Sdoi2014] Count time limit:10 Sec Memory limit:512 MB
submit:404 solved:241
[Submit] [Status] [Discuss] Description
We call a positive integer n is a lucky number when and only if its decimal representation does not contain any element in the number string set S as its substring. For example, when s= (22,333,0233), 233 is the lucky number, 2333, 20233, 3223 is not the lucky number.
Given N and S, calculate the number of lucky numbers not greater than N.
Input
The first line of the input contains the integer n.
The next line is an integer m, which represents the number of elements in S.
The next m line, a number string per line, represents an element in S.
Output
Outputs an integer that represents the value of the answer modulus 109+7.
Sample Input20
3
2
3
14
Sample Output -HINT
The L in the following table represents the length of N, and L represents the sum of all the string lengths in S.
1 < =l < =1200, 1 < =m < =100, 1 < =l < =1500
Source
Round 1 Day 1
[Submit] [Status] [Discuss]
"Bzoj" "3530" "SDOI2014" number