BZOJ 1879: [Sdoi2009]Bill的挑战

暴力枚举 T 的每一位放什么
每一位之间放什么不影响,但是包含什么字符串会互相影响
那么dp状态就得加上字符串的包含情况
\(dp_{i, s}\) 表示当前放第 \(i\) 位, \(s\) 为包含的字符串集合的方案数
\(dp_{0, 2^n-1} = 1\)
转移需要预处理一个数组 \(state_{i, j}\) 表示第 \(i\) 位为字符 \(j\) 时有多少字符串能匹配
这样就可以转移了

#include <bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
#define pii pair<int, int>
#define pli pair<ll, int>
#define lp p << 1
#define rp p << 1 | 1
#define mid ((l + r) >> 1)
#define lowbit(i) ((i) & (-i))
#define ll long long
#define ull unsigned long long
#define db double
#define rep(i,a,b) for(int i=a;i<b;i++)
#define per(i,a,b) for(int i=b-1;i>=a;i--)
#define Edg int ccnt=1,head[N],to[E],ne[E];void addd(int u,int v){to[++ccnt]=v;ne[ccnt]=head[u];head[u]=ccnt;}void add(int u,int v){addd(u,v);addd(v,u);}
#define Edgc int ccnt=1,head[N],to[E],ne[E],c[E];void addd(int u,int v,int w){to[++ccnt]=v;ne[ccnt]=head[u];c[ccnt]=w;head[u]=ccnt;}void add(int u,int v,int w){addd(u,v,w);addd(v,u,w);}
#define es(u,i,v) for(int i=head[u],v=to[i];i;i=ne[i],v=to[i])
const int MOD = 1000003;
void M(int &x) {if (x >= MOD)x -= MOD; if (x < 0)x += MOD;}
int qp(int a, int b = MOD - 2) {int ans = 1; for (; b; a = 1LL * a * a % MOD, b >>= 1)if (b & 1)ans = 1LL * ans * a % MOD; return ans % MOD;}
template<class T>T gcd(T a, T b) { while (b) { a %= b; std::swap(a, b); } return a; }
template<class T>bool chkmin(T &a, T b) { return a > b ? a = b, 1 : 0; }
template<class T>bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
char buf[1 << 21], *p1 = buf, *p2 = buf;
inline char getc() {
	return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++;
}
inline int _() {
	int x = 0, f = 1; char ch = getc();
	while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getc(); }
	while (ch >= '0' && ch <= '9') { x = x * 10ll + ch - 48; ch = getc(); }
	return x * f;
}

const int N = 17, LEN = 55, SS = 1 << N, inv26 = qp(26);
char str[N][LEN];
int dp[LEN][SS], n, k, len;
int state[LEN][27];

void solve() {
	if (k > n) {
		puts("0");
		return;
	}
	memset(dp, 0, sizeof(dp));
	memset(state, 0, sizeof(state));
	int S = 1 << n;
	len = strlen(str[0]);
	rep (i, 0, len)
		rep (j, 0, 26)
			rep (k, 0, n)
				if ((str[k][i] - 'a' == j) || (str[k][i] == '?'))
					state[i][j] |= (1 << k);
	dp[0][S - 1] = 1;
	rep (i, 0, len)
	rep (j, 0, S) if (dp[i][j]) {
		rep (k, 0, 26) {
			M(dp[i + 1][j & state[i][k]] += dp[i][j]);
		}
	}
	int ans = 0;
	rep (i, 0, S) if (__builtin_popcount(i) == k) M(ans += dp[len][i]);
	printf("%d\n", ans);
}

int main() {
#ifdef LOCAL
	freopen("ans.out", "w", stdout);
#endif
	int T;
	sstatef("%d", &T);
	while (T--) {
		sstatef("%d%d", &n, &k);
		rep (i, 0, n) sstatef("%s", str[i]);
		solve();
	}
	return 0;
}
posted @ 2020-03-03 17:44  Mrzdtz220  阅读(103)  评论(0编辑  收藏  举报