"BZOJ1009" "HNOI2008" GT Test AC automata + matrix multiplication

Source: Internet
Author: User

Advertising:
#include <stdio.h>int main(){    puts("转载请注明出处[vmurder]谢谢");    puts("网址:blog.csdn.net/vmurder/article/details/44003109");}
Exercises

The process of establishing an AC automaton can be changed to KMP.
Anyway single string 233.

Code:
#include <queue>#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#define Tusing namespace STD;intN,m,p;structmrx{intX[T][T];} Ini,std,trs,e; MRXoperator* (ConstMRX &a,ConstMRX &b) {MRX c=e;intI,j,k; for(i=0; i<t;i++) for(j=0; j<t;j++) for(k=0; k<t;k++) (c.x[i][j]+= (Long Long) a.x[i][k]*b.x[k][j]%p)%=p;returnC;} MRX Power (MRX X,intp) {MRX ans=std; while(p) {if(p&1) ans=ans*x; x=x*x,p>>=1; }returnAns;}structacauto{intson[t][t],cnt;CharSRC[T];voidBuild_trie () {scanf('%s ', SRC);inti,x=0, ALP; for(i=0; src[i];i++) {alp=src[i]-' 0 ';if(!son[x][alp]) son[x][alp]=++cnt;        X=son[x][alp]; }    } Queue<int>QintFAIL[T];voidBuild_fail () { while(!q.empty ()) Q.pop (); Q.push (1);intI,u,v; while(!q.empty ()) {U=q.front (), Q.pop (); for(i=0;i<Ten; i++) {if(V=son[u][i]) {if(u) fail[v]=son[fail[u]][i];Elsefail[v]=1;                Q.push (v); }ElseSon[u][i]=son[fail[u]][i]; }        }    }voidBuild_mrx () {intI,j,k; for(i=0; i<cnt;i++) for(j=0;j<Ten; j + +) trs.x[son[i][j]][i]++; }}acauto;intMain () {Freopen ("Test.in","R", stdin);scanf("%d%d%d", &n,&m,&p);    Acauto.build_trie ();    Acauto.build_fail (); Acauto.build_mrx (); for(intI=0; i<t;i++) std.x[i][i]=1; ini.x[0][0]=1;    MRX Temp=power (trs,n); Temp=temp*ini;intans=0; for(intI=0; i<m;i++) ans= (ans+temp.x[i][0])%p;printf("%d\n", ans);/ * MRX Tot=ini; for (int i=1;i<=n;i++) tot=trs*tot;*/      return 0;}

"BZOJ1009" "HNOI2008" GT Test AC automata + matrix multiplication

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.