Bzoj 1030 [JSOI2007] text generator (AC automaton)

Source: Internet
Author: User

"Topic link" http://www.lydsy.com/JudgeOnline/problem.php?id=1030

"The main topic"

Find the number of strings that contain any given string

Exercises

We find the quantity that does not contain any given string, and use the complete set to reduce it,
For a given string set up AC automata, with 1 nodes as the root, 0 nodes to 1 even full character set transfer as a super-source,
So 0->match can match all strings that don't contain a given string,
DP[I][J] Indicates the number of strings matched to the I length, matched to the J-node of the AC automaton,
After statistics, take the complement set.

Code

#include <cstdio> #include <algorithm> #include <cstring> using namespace std;const int n=30010;     namespace ac_dfa{const int csize=26;    int tot,son[n][csize],sum[n],fail[n],q[n],match[n];         void Initialize () {memset (match,0,sizeof (int) * (tot+1));        memset (fail,0,sizeof (int) * (tot+1));        memset (sum,0,sizeof (int) * (tot+1));        for (int i=0;i<=tot;i++) for (int j=0;j<csize;j++) son[i][j]=0;     Tot=1;    } inline int Tr (char ch) {return ch-' A ';}        int Insert (char *s) {int x=1;            for (int L=strlen (s), i=0,w;i<l;i++) {if (!son[x][w=tr (S[i])]) {Son[x][w]=++tot;         }X=SON[X][W];        }sum[x]++;    return x;        } void Makefail () {int h=1,t=0,i,j,x=1; q[++t]=1; while (h<=t) for (x=q[h++],i=0;i<csize;i++) if (Son[x][i]) {Fail[son[x][i]]=son[fail[x]][i],q[++t]=son [x]            [i];        Match[son[x][i]]=sum[son[x][i]]?son[x][i]:match[fail[son[x][i]]; }else Son[x][i]=son[fail[x]][i];        }}using namespace Ac_dfa;const int p=10007;int Dp[110][n];char s[n];void dp (int x) {for (int i=1;i<=tot;i++) { if (match[i]| |!        Dp[x-1][i]) continue;            for (int j=0;j<csize;j++) {int k=i;            while (!son[k][j]) k=fail[k];        Dp[x][son[k][j]]= (Dp[x][son[k][j]]+dp[x-1][i])%P; }}}int n,m;    int main () {scanf ("%d%d", &n,&m);    Initialize ();    for (int i=0;i<csize;i++) son[0][i]=1;    for (int i=1;i<=n;i++) {scanf ("%s", s); Insert (s);}    Makefail ();     int ans1=0,ans2=1;    Dp[0][1]=1;    for (int i=1;i<=m;i++) Dp (i);    for (int i=1;i<=m;i++) ans2= (ans2*26)%P;    for (int i=1;i<=tot;i++) if (!match[i]) ans1= (ans1+dp[m][i])%P;    printf ("%d\n", (ans2-ans1+p)%P); return 0;}

Bzoj 1030 [JSOI2007] text generator (AC automaton)

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.