字符串
题目背景:
分析:AC自动机 + DP
显然,对于一个匹配串,有4种可能的出现情况,在前一半出现,在后一半出现,跨过中间在前一半较多,跨过中间在后一半较多,对于一个串,如果它在后一半出现,说明将这个串翻转再按位取反之后会出现在前一半,那么,对于前两种情况就只需要在AC自动机上走m步,状压DP一下就可以了,而对于第三种情况,我们只需要对于正串的每一个大于一半的前缀判断一下如果以当前为第m个字符,后面部分能否是可以的取反情况,第四种情况同理,最后我们只需要枚举第m个位置在哪里,然后将对应的dp状态和可以以当前节点作为结尾的情况取一个并集就可以了。注意需要将fail指针上的标记,与自身标记取并,表示如果当前节点可以到达,那么fail指针上的标记一定也能够满足,状态就是dp[i][j][s]表示当前走了i步,在第j个AC自动机的节点上,当前有s状态中的串可以匹配。直接枚举第i + 1位是0还是1就可以了。
Source:
/*
created by scarlyw
*/
#include <cstdio>
#include <string>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <cmath>
#include <cctype>
#include <vector>
#include <set>
#include <queue>
#include <ctime>
#include <bitset>
inline char read() {
static const int IN_LEN = 1024 * 1024;
static char buf[IN_LEN], *s, *t;
if (s == t) {
t = (s = buf) + fread(buf, 1, IN_LEN, stdin);
if (s == t) return -1;
}
return *s++;
}
///*
template<class T>
inline void R(T &x) {
static char c;
static bool iosig;
for (c = read(), iosig = false; !isdigit(c); c = read()) {
if (c == -1) return ;
if (c == '-') iosig = true;
}
for (x = 0; isdigit(c); c = read())
x = ((x << 2) + x << 1) + (c ^ '0');
if (iosig) x = -x;
}
//*/
const int OUT_LEN = 1024 * 1024;
char obuf[OUT_LEN], *oh = obuf;
inline void write_char(char c) {
if (oh == obuf + OUT_LEN) fwrite(obuf, 1, OUT_LEN, stdout), oh = obuf;
*oh++ = c;
}
template<class T>
inline void W(T x) {
static int buf[30], cnt;
if (x == 0) write_char('0');
else {
if (x < 0) write_char('-'), x = -x;
for (cnt = 0; x; x /= 10) buf[++cnt] = x % 10 + 48;
while (cnt) write_char(buf[cnt--]);
}
}
inline void flush() {
fwrite(obuf, 1, oh - obuf, stdout);
}
/*
template<class T>
inline void R(T &x) {
static char c;
static bool iosig;
for (c = getchar(), iosig = false; !isdigit(c); c = getchar())
if (c == '-') iosig = true;
for (x = 0; isdigit(c); c = getchar())
x = ((x << 2) + x << 1) + (c ^ '0');
if (iosig) x = -x;
}
//*/
const int MAXN = 1200 + 10;
const int MAXM = 500 + 10;
const int MAXX = 6;
const int mod = 998244353;
int n, m, cnt, ans;
int f[MAXM][MAXN][1 << MAXX | 1];
char s[MAXN];
inline void add(int &x, int t) {
x += t, (x >= mod) ? (x -= mod) : 0;
}
struct node {
int c[2];
int end, tag, fail;
} t[MAXN];
inline bool check(int pos, int len) {
for (int i = pos + 1; i < len; ++i)
if (pos - (i - pos) + 1 < 0 || s[pos - (i - pos) + 1] == s[i])
return false;
return true ;
}
inline void insert(int id, char *s) {
int pos = 0, len = strlen(s);
for (int i = 0; i < len; ++i) {
if (t[pos].c[s[i] - '0'] == 0)
t[pos].c[s[i] - '0'] = ++cnt;
pos = t[pos].c[s[i] - '0'];
if (check(i, len)) t[pos].tag |= (1 << id - 1);
}
t[pos].end |= (1 << id - 1);
}
inline void build_fail() {
std::queue<int> q;
q.push(0);
while (!q.empty()) {
int cur = q.front(), s0 = t[cur].c[0], s1 = t[cur].c[1];
int last = t[cur].fail;
q.pop();
if (s0) {
if (cur) t[s0].fail = t[last].c[0];
q.push(s0), t[s0].end |= t[t[s0].fail].end;
t[s0].tag |= t[t[s0].fail].tag;
} else t[cur].c[0] = t[last].c[0];
if (s1) {
if (cur) t[s1].fail = t[last].c[1];
q.push(s1), t[s1].end |= t[t[s1].fail].end;
t[s1].tag |= t[t[s1].fail].tag;
} else t[cur].c[1] = t[last].c[1];
}
}
inline void get_dp() {
f[0][0][0] = 1;
for (int i = 1; i <= m; ++i)
for (int pos = 0; pos <= cnt; ++pos)
for (int cur = 0, s = (1 << n); cur < s; ++cur) {
int u, v;
u = t[pos].c[0];
v = cur | t[u].end;
add(f[i][u][v], f[i - 1][pos][cur]);
u = t[pos].c[1];
v = cur | t[u].end;
add(f[i][u][v], f[i - 1][pos][cur]);
}
for (int pos = 0; pos <= cnt; ++pos)
for (int cur = 0, s = (1 << n); cur < s; ++cur)
if ((cur | t[pos].tag) == s - 1)
add(ans, f[m][pos][cur]);
std::cout << ans;
}
inline void read_in() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; ++i) {
scanf("%s", s);
insert(i, s);
int len = strlen(s);
std::reverse(s, s + len);
for (int j = 0; j < len; ++j) s[j] ^= 1;
insert(i, s);
}
}
int main() {
freopen("string.in", "r", stdin);
freopen("string.out", "w", stdout);
read_in();
build_fail();
get_dp();
return 0;
}