Question: Given the number string s of length m, the number of number strings that do not contain the substring s of length n
N<=10^9 Light Look at this O (n) is hanging
We do not consider the number of schemes that match the last J bit in the number string of length I with the first J bit in S of the f[i][j]
For example, when S is 12312 f[i][3] indicates the number of scenarios with a length of I, ending with 123, and not containing the substring "12312"
A[x][y] The number of scenarios transferred to F[i][y] for F[i-1][x]
In other words (may not be described clearly) a[x][y] for s the prefix of length x plus a number suffix can match the longest length of the prefix of y this number can have how many kinds
For example 12312 This number string generates an array of a (array starting from 0):
9 1 0 0 0 0
8 1 1 0 0 0
8 1 0 1 0 0
9 0 0 0 1 0
8 1 0 0 0 1
A[2][1]=1 represents a prefix of length 2 plus a ' 1 ' after matching up to a prefix of length 1
A[4][0]=8 indicates that a prefix of length 4 and a number other than ' 1 ' 2 ' become the prefix of length 0.
But A[x][5] represents an exact match, does not meet the requirements of the test instructions, so we do not consider this column matrix multiplication
We find that f[i-1] by this matrix becomes the f[i] how is this matrix to be asked? KMP algorithm, for each length of the prefix enumeration next character to transfer the specific wording see the code
F Initial value is f[0][0]=1,f[0][x]=0 (x>0)
So in the end we just need to take the first line of the answer matrix
Went to the internet to find a bunch of the puzzle to understand 0.0 here to write a little more detailed it
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm>using namespace Std;struct matrix{int xx[22][22];int* operator [] (int x) {return xx[x];}} A,b;int N,m,p,ans;char s[100];int next[100];void operator *= (Matrix &x,matrix &y) {int I,j,k;matrix z;memset (& Amp;z,0,sizeof z); for (i=0;i<m;i++) for (j=0;j<m;j++) for (k=0;k<m;k++) z[i][j]+=x[i][k]*y[k][j],z[i][j]%=p; X=z;} void KMP () {int I,fix=0;char j;for (i=2;i<=m;i++) {while (fix && s[fix+1]!=s[i]) fix=next[fix];if (s[fix+1]==s [i]) ++fix;next[i]=fix;} for (i=0;i<m;i++) for (j= ' 0 '; j<= ' 9 '; j + +) {fix=i;while (fix && s[fix+1]!=j) fix=next[fix];if (j==s[fix+1]) B[i][fix+1]++;else b[i][0]++;}} void Quick_power (int x) {while (x) {if (x&1) a*=b;b*=b;x>>=1;}} int main () {int i;cin>>n>>m>>p;scanf ("%s", s+1); KMP (); for (i=0;i<m;i++) a[i][i]=1; Quick_power (n); for (i=0;i<m;i++) Ans+=a[0][i],ans%=p;cout<<ans<<endl;}
BZOJ 1009 HNOI2008 GT exam KMP algorithm + matrix multiplication