洛谷传送门
BZOJ传送门
题目描述
输入输出格式
输入格式:
本题包含多组数据。 第一行:一个整数 T T ,表示数据的个数。 对于每组数据: 第一行:两个整数,和 K K (含义如题目表述)。 接下来行:每行一个字符串。
输出格式:
如题
输入输出样例
输入样例#1:
5
3 3
???r???
???????
???????
3 4
???????
?????a?
???????
3 3
???????
?a??j??
????aa?
3 2
a??????
???????
???????
3 2
???????
???a???
????a??
输出样例#1:
914852
0
0
871234
67018
说明
对于30%的数据, T≤5 T ≤ 5 , M≤5 M ≤ 5 ,字符串长度 ≤20 ≤ 20 ;
对于70%的数据, T≤5 T ≤ 5 , M≤13 M ≤ 13 ,字符串长度 ≤30 ≤ 30 ;
对于100%的数据, T≤5 T ≤ 5 , M≤15 M ≤ 15 ,字符串长度 ≤50 ≤ 50 。
解题分析
看这数据范围, 一脸状压的样子。
我们可以先处理出 mat[i][ch] m a t [ i ] [ c h ] 表示第 i i 位上放的匹配情况, 第 j j 位的表示其能否匹配。
然后我们按位填字符, 暴力转移即可。 即: dp[i+1][state d p [ i + 1 ] [ s t a t e & mat[i][ch]]+=dp[i][state] m a t [ i ] [ c h ] ] + = d p [ i ] [ s t a t e ]
最后我们在 dp[len][i] d p [ l e n ] [ i ] 里面选出 i i 数位上有个1的数的 dp d p 值加起来就可以了。( __builtin _ _ b u i l t i n 贼6)
代码如下:
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cctype>
#include <algorithm>
#include <cmath>
#define R register
#define IN inline
#define gc getchar()
#define W while
#define MOD 1000003
#define MX (1 << 15)
char str[20][55];
int mat[55][30], dp[55][MX];
int T, len, n, K, ans, res;
int main(void)
{
R int i, j, k;
scanf("%d", &T);
W (T--)
{
scanf("%d%d", &n, &K);
std::memset(mat, ans = 0, sizeof(mat));
std::memset(dp, 0, sizeof(dp));
for (i = 1; i <= n; ++i) scanf("%s", str[i]);
len = std::strlen(str[1]);
for (i = 0; i < len; ++i)
for (j = 0; j < 26; ++j)
for (k = 1; k <= n; ++k)
if(str[k][i] == 'a' + j || str[k][i] == '?') mat[i][j] |= (1 << k - 1);//预处理mat数组
int bd = (1 << n) - 1;
dp[0][bd] = 1;//使第一次无论填什么都可以
for (i = 0; i < len; ++i)
for (j = 0; j <= bd; ++j)
if(dp[i][j]) for (k = 0; k < 26; ++k) (dp[i + 1][j & mat[i][k]] += dp[i][j]) %= MOD;
for (i = 1; i <= bd; ++i) if(__builtin_popcount(i) == K) (ans += dp[len][i]) %= MOD;
printf("%d\n", ans);
}
}