hdu 2825 Wireless Password AC自动机+状态DP
时间卡得紧,写成递推可以做一些优化
//#pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #include<iostream> #include<sstream> #include<cmath> #include<climits> #include<string> #include<map> #include<queue> #include<vector> #include<stack> #include<set> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int,int> pii; #define pb(a) push_back(a) #define INF 0x1f1f1f1f #define lson idx<<1,l,mid #define rson idx<<1|1,mid+1,r #define PI 3.1415926535898 template<class T> T min(const T& a,const T& b,const T& c) { return min(min(a,b),min(a,c)); } template<class T> T max(const T& a,const T& b,const T& c) { return max(max(a,b),max(a,c)); } void debug() { #ifdef ONLINE_JUDGE #else freopen("d:\\in.txt","r",stdin); // freopen("d:\\out1.txt","w",stdout); #endif } int getch() { int ch; while((ch=getchar())!=EOF) { if(ch!=' '&&ch!='\n')return ch; } return EOF; } const int MAX_NODE=100; const int SIGMA_SIZE=26; const int mod = 20090717; int ch[MAX_NODE][SIGMA_SIZE]; int val[MAX_NODE]; int fail[MAX_NODE]; int sz; void init() { memset(ch[0],0,sizeof(ch[0])); sz=1; val[0]=0; } int idx(char c){return c-'a';} void insert(const char *s,int k) { int u=0; for(int i=0;s[i]!='\0';i++) { int v=idx(s[i]); if(!ch[u][v]) { memset(ch[sz],0,sizeof(ch[sz])); val[sz]=0; ch[u][v]=sz++; } u=ch[u][v]; } val[u]=1<<k; } void construct () { fail[0]=0; queue<int> q; for(int c=0;c<SIGMA_SIZE;c++) { int u=ch[0][c]; if(u){fail[u]=0;q.push(u);} } while(!q.empty()) { int r=q.front();q.pop(); for(int c=0;c<SIGMA_SIZE;c++) { int u=ch[r][c]; if(!u) { ch[r][c]=ch[fail[r]][c]; continue; } q.push(u); int v=fail[r]; while(v&&!ch[v][c])v=fail[v]; fail[u]=ch[v][c]; val[u]|=val[fail[u]]; } } } int check[1<<10]; void prework() { for(int st=0;st<(1<<10);st++) { check[st]=0; int nst=st; while(nst) { check[st]+=nst%2; nst/=2; } } } int dp[MAX_NODE][26][1<<10]; int DP(int n,int m,int k) { memset(dp,0,sizeof(dp)); dp[0][0][0]=1; for(int i=0,k=0;i<n;i++,k^=1) { for(int u = 0;u<sz;u++) { for(int st=0;st<(1<<m);st++)if(dp[u][i][st]>0) { for(int c=0;c<SIGMA_SIZE;c++) { dp[ch[u][c]][i+1][st|val[ch[u][c]]]=(dp[ch[u][c]][i+1][st|val[ch[u][c]]]+dp[u][i][st])%mod; } } } } int res=0; for(int u=0;u<sz;u++) { for(int st=(1<<k)-1;st<=(1<<m);st++) if(check[st]>=k) res=(res+dp[u][n][st])%mod; } return res; } int main() { prework(); int n,m,k; while(scanf("%d%d%d",&n,&m,&k)!=EOF&&(n+m+k)) { init(); for(int i=1;i<=m;i++) { char buf[15]; scanf("%s",buf); insert(buf,i-1); } construct(); int res=DP(n,m,k); printf("%d\n",res); } return 0; }