天天看點

HDU 2825 Wireless Password

Problem Description

Liyuan lives in a old apartment. One day, he suddenly found that there was a wireless network in the building. Liyuan did not know the password of the network, but he got some important information from his neighbor. He knew the password consists only of lowercase letters 'a'-'z', and he knew the length of the password. Furthermore, he got a magic word set, and his neighbor told him that the password included at least k words of the magic word set (the k words in the password possibly overlapping).

For instance, say that you know that the password is 3 characters long, and the magic word set includes 'she' and 'he'. Then the possible password is only 'she'.

Liyuan wants to know whether the information is enough to reduce the number of possible passwords. To answer this, please help him write a program that determines the number of possible passwords.

Input

There will be several data sets. Each data set will begin with a line with three integers n m k. n is the length of the password (1<=n<=25), m is the number of the words in the magic word set(0<=m<=10), and the number k denotes that the password included at least k words of the magic set. This is followed by m lines, each containing a word of the magic set, each word consists of between 1 and 10 lowercase letters 'a'-'z'. End of input will be marked by a line with n=0 m=0 k=0, which should not be processed.

Output

For each test case, please output the number of possible passwords MOD 20090717.

Sample Input

10 2 2

hello

world

4 1 1

icpc

10 0 0

0 0 0

Sample Output

2

1

14195065

#include<stdio.h>
#include<string.h>
#include<queue>
#include<malloc.h>
using namespace std;
const int maxn = 1000005;
const int base = 20090717;
int n, m, K, f[2][105][1024];

class tire
{
public:
    tire *down[26], *next;
    int end, cnt;
    tire(){ next = NULL; cnt = end = 0; memset(down, 0, sizeof(down)); }
    void assign(){ next = NULL; end = 0; memset(down, 0, sizeof(down)); }
};

class ac_automaton
{
private:
    tire *root;
    tire *node[105];
    int tot;
    char s[maxn];
    char S[maxn];
    int mask[2048];
public:
    ac_automaton(){ 
        node[0] = root = new tire; tot = 0;
        memset(mask, 0, sizeof(mask));
        for (int i = 0; i < 1024; i++)
            mask[i] = mask[i >> 1] + (i & 1);
    }
    void clear(){ node[0] = root = new tire; tot = 0; }

    void insert(int x)
    {
        scanf("%s", s);
        tire* j = root;
        for (int i = 0, k; s[i]; i++)
        {
            k = s[i] - 'a';
            if (!j->down[k])
            {
                node[++tot] = j->down[k] = new tire;
                node[tot]->cnt = tot;
            }
            j = j->down[k];
        }
        j->end = 1 << (x - 1);
    }

    void getnext()
    {
        queue<tire*> p;
        tire *q, *k, *j;

        root->next = root;
        for (int i = 0; i < 26; i++)
        if (root->down[i])
        {
            q = root->down[i];
            q->next = root;
            p.push(q);
        }
        else root->down[i] = root;

        while (!p.empty())
        {
            q = p.front();    p.pop();
            k = q->next;

            for (int i = 0; i < 26; i++)
            if (q->down[i])
            {
                j = q->down[i];
                j->next = k->down[i];
                j->end |= k->down[i]->end;
                p.push(q->down[i]);
            }
            else q->down[i] = k->down[i];
        }
    }

    int getmother() { scanf("%s", S); root = new tire; return strlen(S); }

    int work_out()
    {
        getnext();
        memset(f[0], 0, sizeof(f[0]));
        f[0][0][0] = 1;
        int M = (1 << m), u = 0;
        tire *a, *b;
        for (int i = 0; i < n; i++)
        {
            u = (u + 1) & 1;
            for (int j = 0; j <= tot; j++)
                for (int k = 0; k < M; k++) f[u][j][k] = 0;

            for (int j = 0; j <= tot; j++)
                for (int k = 0; k < M; k++)
                    if (f[u ^ 1][j][k])
                    {
                        a = node[j];
                        for (int v = 0; v < 26; v++)
                        {
                            b = a->down[v];
                            (f[u][b->cnt][b->end | k] += f[u ^ 1][j][k]) %= base;
                        }
                    }
        }
        int ans = 0;
        for (int i = 0; i <= tot; i++)
            for (int j = 0; j < M; j++)
                if (mask[j] >= K) ans = (ans + f[u][i][j]) % base;
        return ans;
    }
}F;

int main()
{
    while (scanf("%d%d%d", &n, &m, &K), n + m + K)
    {
        F.clear();
        for (int i = 0; i < m; i++) F.insert(i + 1);
        printf("%d\n", F.work_out());
    }
    return 0;
}