HDU 2825 Wireless Password-AC + DP

Source: Internet
Author: User

Give m words. A new word consisting of m words (two words can overlap) has a length of n and contains no less than k basic words. How many new words are there?

 


M is very small, and binary is used to indicate that a new word contains a basic word.

Using m words to create an AC automatic machine, you can find out the mutual inclusion of all words. The suffix feature of the AC automatic machine (the mismatched edge of each node points to a new node, the string from the new node to the trie root is the suffix of the current node string ).

 


Dynamic Planning:

F (I, j, k): a word with the length of I. k (Binary) is contained at the j node in the trie tree ), total number of solutions.

 


Transfer equation:

Add a letter to the end of the current string.

F (I + 1, u, k | val [u]) + = f (I, j, k)

U is the child node of the j node (or the node pointed by the mismatched edge). k | val [u] indicates the situation where a new letter is added and the basic word is contained.

 

# Include <cstdio> # include <cstring> # include <algorithm> # include <queue> using namespace std; struct AC_Automata {# define N 102 # define M 26 int ch [N] [M], val [N], last [N], f [N], sz; void clear () {sz = 1; memset (ch [0], 0, sizeof (ch [0]);} int idx (char c) {return c-'A';} void insert (char s [], int v) {int u = 0; for (int I = 0; s [I]; I ++) {int c = idx (s [I]); if (! Ch [u] [c]) {memset (ch [sz], 0, sizeof (ch [sz]); val [sz] = 0; ch [u] [c] = sz ++;} u = ch [u] [c];} val [u] = 1 <v;} void build () {queue <int> q; f [0] = 0; for (int c = 0; c <M; c ++) {int u = ch [0] [c]; if (u) {f [u] = last [u] = 0; q. push (u) ;}} while (! Q. empty () {int r = q. front (); q. pop (); for (int c = 0; c <M; c ++) {int u = ch [r] [c]; if (! U) {ch [r] [c] = ch [f [r] [c]; val [r] = val [r] | val [f [r]; // If the suffix of a string from the root node to the current node contains basic words (PASS) continue;} q. push (u); f [u] = ch [f [r] [c]; last [u] = val [f [u]? F [u]: last [f [u] ;}}} ac; int c [1030]; /// record each status containing several strings int f [27] [102] [1025]; void init () {memset (c, 0, sizeof (c )); for (int I = 0; I <1024; I ++) {for (int j = 0; j <10; j ++) if (1 <j) & I) c [I] ++ ;}} void solve (int n, int m, int p) {# define Mod 20090717 int u; for (int I = 0; I <= n; I ++) for (int j = 0; j <ac. sz; j ++) for (int k = 0; k <(1 <m); k ++) f [I] [j] [k] = 0; f [0] [0] [0] = 1; for (int I = 0; I <n; I ++) for (int j = 0; j <ac. sz; j ++) {for (int k = 0; k <(1 <m); k ++) {if (f [I] [j] [k] = 0) continue; for (int jj = 0; jj <M; jj ++) {u = ac. ch [j] [jj]; f [I + 1] [u] [ac. val [u] | k] = (f [I + 1] [u] [ac. val [u] | k] + f [I] [j] [k]) % Mod ;}} int ans = 0; for (int I = 0; I <ac. sz; I ++) for (int j = 0; j <(1 <m); j ++) if (c [j]> = p) ans = (ans + f [n] [I] [j]) % Mod; printf ("% d \ n", ans);} int main () {int n, m, k; char s [12]; init (); while (scanf ("% d", & n, & m, & k) = 3) {if (n = 0 & m = 0 & k = 0) break; ac. clear (); for (int I = 0; I <m; I ++) {scanf ("% s", s); ac. insert (s, I);} ac. build (); solve (n, m, k);} return 0 ;}

 

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.