传送门:洛谷 - P4052
思路分析
求至少包含一个单词的串不好求,可以求一个都不包含的,然后用总数减去
如果一个点的fail包含,那么这个点也包含,标记一下
$dp[i][j]$表示匹配前i个字母,当前在ac自动机的第j个节点,一个都不包含的方案数
AC代码
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define fun function
#define sz(x) (int)(x).size()
#define lowbit(x) (x)&(-x)
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
namespace FastIO {
#define BUF_SIZE 100000
#define OUT_SIZE 100000
bool IOerror=0;
inline char nc() {
static char buf[BUF_SIZE],*p1=buf+BUF_SIZE,*pend=buf+BUF_SIZE;
if(p1==pend) {
p1=buf;
pend=buf+fread(buf,1,BUF_SIZE,stdin);
if(pend==p1) {
IOerror=1;
return -1;
}
}
return *p1++;
}
inline bool blank(char ch) {
return ch==' '||ch=='\n'||ch=='\r'||ch=='\t';
}
template<class T> inline bool read(T &x) {
bool sign=0;
char ch=nc();
x=0;
for(; blank(ch); ch=nc());
if(IOerror)return false;
if(ch=='-')sign=1,ch=nc();
for(; ch>='0'&&ch<='9'; ch=nc())x=x*10+ch-'0';
if(sign)x=-x;
return true;
}
template<class t> inline void print(t x) {
if (x < 0) putchar('-'), print(-x);
else {
if (x > 9) print(x / 10);
putchar('0' + x % 10);
}
}
template<class T,class... U>bool read(T& h,U&... t) {
return read(h)&&read(t...);
}
#undef OUT_SIZE
#undef BUF_SIZE
};
using namespace std;
using namespace FastIO;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef long long ll;
typedef pair<int,int> pii;
typedef unsigned long long ull;
const int INF = 0x3f3f3f3f;
const int N = 1e6+10;
const int mod = 1e4+7;
struct AC_auto {
struct T {
int son[26],fail,word,last;
void clear() {
mem(son,0);
word = fail = last = 0;
}
} tree[N];
int cnt = 1;
int vis[N] ;
int dp[110][100010];
int newnode() {
++cnt;
tree[cnt].clear();
return cnt;
}
void init(){
cnt = 1;
tree[1].clear();
tree[1].word = 1;
}
void ins(string s) {
int p = 1;
for(auto i:s) {
int c = i-'A';
if(!tree[p].son[c]) tree[p].son[c] = newnode();
p = tree[p].son[c];
}
tree[p].word = 1;
vis[p] = 1;
}
void getfail() {
for(int i=0; i<26; i++) tree[0].son[i] = 1;
queue<int>q;
q.push(1);
while(!q.empty()) {
int u = q.front();
q.pop();
int ufail = tree[u].fail;
tree[u].last = tree[ufail].word>0?ufail:tree[ufail].last;
for(int i=0; i<26; i++) {
int v = tree[u].son[i];
if(v) {
tree[v].fail = tree[ufail].son[i];
vis[v]|=vis[tree[v].fail];
q.push(v);
} else tree[u].son[i] = tree[ufail].son[i];
}
}
}
int solve(int m){
dp[0][1] = 1;
for(int i=1;i<=m;i++){
for(int j=1;j<=cnt;j++){
for(int k=0;k<26;k++){
int v = tree[j].son[k];
if(!vis[v]) dp[i][v] = (dp[i][v]+dp[i-1][j])%mod;
}
}
}
int res = 1;
for(int i=1;i<=m;i++) res = (res*26)%mod;
for(int i=1;i<=cnt;i++) res = (res - dp[m][i] + mod) %mod;
return res;
}
}AC;
signed main() {
#ifdef xiaofan
freopen("1.in","r",stdin);
freopen("1.out","w",stdout);
#endif
int n,m;
cin>>n>>m;
AC.init();
for(int i=1;i<=n;i++){
string s;
cin>>s;
AC.ins(s);
}
AC.getfail();
cout<<AC.solve(m)<<endl;
return 0;
}