AC自动机上套dp
首先是通过给定的 blacklist 建立AC自动机,那么合法的 password 在生成过程中肯定不能经过任意一个结束点。于是在这个自动机上写状压dp,0~7用三位分别表示含有数字、小写字母、大写字母的状态的合法方案数。同时要注意一点,把所有不能经过的点标位不合法点,那么如果一个节点不合法,它的子树的所有节点都是不合法的,而且如果一个节点的fail指针指向一个不合法节点,那么这个节点也是不合法的,不合法的节点不能进行状态转移,最后把所有节点状态为7的加起来就是当前长度的答案了。
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
using namespace std;
const int mo = 1000003;
const int MAX=27;
typedef struct Trie_Node {
Node() {}
struct Trie_Node *fail;
struct Trie_Node *next[MAX];
int f[2][8];
int count;
}Trie;
Trie *Q[500010];
char keyword[55];
char str[1000010];
int head, tail;
int l, r, n, k, sum;
void insert(Trie *root,char *word) {
Trie *p = root;
int i = 0;
while (word[i] != '\0') {
if(p->next[word[i]-'a'] == NULL) {
Trie *temp = new Trie;
for(int j = 0; j < MAX; j++) temp->next[j] = NULL;
temp->count = 0;
temp->fail = NULL;
memset(temp->f, 0, sizeof(temp->f));
p->next[word[i]-'a'] = temp;
}
p = p->next[word[i]-'a'];
i++;
}
p->count++;
}
void build_ac(Trie *root) {
root->fail = NULL;
head = tail = 0;
Q[head++] = root;
while(head != tail) {
Trie *temp = Q[tail++];
Trie *p = NULL;
for(int i = 0; i < MAX; i++) {
if(temp->next[i] != NULL) {
temp->next[i]->count += temp->count;
if (temp == root) temp->next[i]->fail = root;
else {
p = temp->fail;
while(p != NULL) {
if(p->next[i] != NULL) {
temp->next[i]->fail = p->next[i];
break;
}
p = p->fail;
}
if (p == NULL) temp->next[i]->fail = root;
}
temp->next[i]->count += temp->next[i]->fail->count;
Q[head++] = temp->next[i];
}
}
}
}
void clean(Trie *x, int k) {
memset(x->f[k], 0, sizeof(x->f[k]));
for (int i = 0; i < MAX; i++) if (x->next[i] != NULL) {
clean(x->next[i], k);
}
}
void count(Trie *x, int k) {
sum = (sum+x->f[k][7])%mo;
for (int i = 0; i < MAX; i++) if (x->next[i] != NULL) count(x->next[i], k);
}
const int dir[10] = {14, 8, 26, 4, 26, 18, 26, 19, 26, 26};
void dfs(Trie *x, int k) {
for (int state = 0; state <= 7; state++) if (x->f[1-k][state]) {
for (int i = 0; i < 26; i++) {
Trie *temp = x;
while (temp->fail != NULL && temp->next[i] == NULL) temp = temp->fail;
if (temp->next[i] != NULL) temp = temp->next[i];
if (temp->count) continue;
//x => temp
temp->f[k][state|1] = (temp->f[k][state|1]+x->f[1-k][state])%mo;
temp->f[k][state|2] = (temp->f[k][state|2]+x->f[1-k][state])%mo;
}
for (int i = 0; i < 10; i++) {
Trie *temp = x;
while (temp->fail != NULL && temp->next[dir[i]] == NULL) temp = temp->fail;
if (temp->next[dir[i]] != NULL) temp = temp->next[dir[i]];
if (temp->count) continue;
temp->f[k][state|4] = (temp->f[k][state|4]+x->f[1-k][state])%mo;
}
}
for (int i = 0; i < MAX; i++) if (x->next[i] != NULL) dfs(x->next[i], k);
}
int main() {
scanf("%d%d%d",&l,&r,&n);
Trie *root = new Trie;
root->fail = NULL;
root->count = 0;
memset(root->f, 0, sizeof(root->f));
for(int i = 0; i < MAX; i++) root->next[i] = NULL;
for (int i = 1; i <= n; i++) {
scanf("%s", &keyword);
insert(root, keyword);
}
sum = 0;
build_ac(root);
k = 1;
root->f[k][0] = 1;
for (int i = 1; i <= r; i++) {
k = 1-k;
clean(root, k);
dfs(root, k);
if (i >= l) count(root, k);
}
printf("%d\n", sum);
}