题目链接:点击这里
题意:求所有长度在 L 之内的出现至少一种模式串的文本串个数. 对 264 取模.
对 264 取模可以简单的看成无符号64位整数的自然溢出, 然后就可以忽略取模了. 和这题类似. 求出所有不存在模式串的文本串然后减一下就好了.
先用AC自动机插入所有的文本串, 然后就在自动机上走找所有的不存在模式串的文本串, 建立矩阵 A , 因为是长度小于等于 L , 所以要求出
A1+A2+A3+...+AL
然后0能够到达的种数加起来就好了.
#include
#include
#include
#include
#include
#include
using namespace std;
#define maxn 44
int n;
long long m;
char str[11][11];
int tot;
struct M {
unsigned long long a[maxn][maxn];
M () {
memset (a, 0, sizeof a);
}
M operator + (const M &gg) const {
M ans; memset (ans.a, 0, sizeof ans.a);
for (int i = 0; i < tot; i++) {
for (int j = 0; j < tot; j++) {
ans.a[i][j] = a[i][j] + gg.a[i][j];
}
}
return ans;
}
M operator * (const M &gg) const {
M ans; memset (ans.a, 0, sizeof ans.a);
for (int i = 0; i < tot; i++) {
for (int j = 0; j < tot; j++) {
for (int l = 0; l < tot; l++) {
ans.a[i][j] += a[i][l]*gg.a[l][j];
}
}
}
return ans;
}
void show () {
for (int i = 0; i < tot; i++) {
for (int j = 0; j < tot; j++)
cout << a[i][j] << " ";
cout << endl;
}
}
};
M qpow (M a, long long k) {
M ans;
int i, j;
for (i = 0; i < tot; ++i)
for (j = 0; j < tot; ++j)
ans.a[i][j] = (i == j ? 1 : 0);
for(; k; k >>= 1) {
if (k&1) ans = ans*a;
a = a*a;
}
return ans;
}
M cal (M a, long long n) {
if (n == 1) {
return a;
}
M ans = cal (a, n>>1);
M tmp = qpow (a, n>>1);
if (n&1) {
tmp = tmp * a;
}
ans = ans + ans*tmp;
if (n&1) {
ans = ans + tmp;
}
return ans;
}
struct trie {
int next[maxn][26], fail[maxn], end[maxn];
int root, cnt;
int new_node () {
memset (next[cnt], -1, sizeof next[cnt]);
end[cnt++] = 0;
return cnt-1;
}
void init () {
cnt = 0;
root = new_node ();
}
void insert (char *buf) {
int len = strlen (buf);
int now = root;
for (int i = 0; i < len; i++) {
int id = buf[i]-'a';
if (next[now][id] == -1) {
next[now][id] = new_node ();
}
now = next[now][id];
}
end[now]++;
}
void build () {
queue <int> q;
fail[root] = root;
for (int i = 0; i < 26; i++) {
if (next[root][i] == -1) {
next[root][i] = root;
}
else {
fail[next[root][i]] = root;
q.push (next[root][i]);
}
}
while (!q.empty ()) {
int now = q.front (); q.pop ();
for (int i = 0; i < 26; i++) {
if (next[now][i] == -1) {
next[now][i] = next[fail[now]][i];
}
else {
fail[next[now][i]] = next[fail[now]][i];
q.push (next[now][i]);
}
}
}
}
int f[maxn];
unsigned long long query () {
M ans; memset (ans.a, 0, sizeof ans.a);
memset (f, -1, sizeof f);
tot = 0;
for (int i = 0; i < cnt; i++) if (!end[i]) {
bool flag = 1;
int tmp = i;
while (tmp != root) {
if (end[tmp]) {
flag = 0;
break;
}
tmp = fail[tmp];
}
if (flag) {
f[i] = tot++;
}
}
for (int i = 0; i < cnt; i++) if (f[i] != -1) {
int u = f[i];
for (int id = 0; id < 26; id++) {
int v = next[i][id];
if (f[v] != -1) {
v = f[v];
ans.a[u][v]++;
}
}
}
ans = cal (ans, m);
unsigned long long res = 0;
for (int i = 0; i < tot; i++)
res += ans.a[0][i];
return res;
}
}ac;
unsigned long long qpow (unsigned long long a, long long b) {
if (b == 0)
return 1;
unsigned long long ans = qpow (a, b>>1);
ans *= ans;
if (b&1)
ans *= a;
return ans;
}
unsigned long long get (long long n) {
if (n == 1)
return 26;
unsigned long long ans = get (n>>1), tmp = qpow (26, n>>1);
if (n&1) {
tmp *= 26;
}
ans = ans + tmp*ans;
if (n&1) {
ans += tmp;
}
return ans;
}
int main () {
while (scanf ("%d%lld", &n, &m) == 2) {
unsigned long long num = get (m);
ac.init ();
for (int i = 1; i <= n; i++) {
scanf ("%s", str[i]);
ac.insert (str[i]);
}
ac.build ();
unsigned long long ans = ac.query ();
cout << num-ans << "\n";
}
return 0;
}