「SDOI2009」Bill的挑战

传送门

状压 \(\text{DP}\)

瞄一眼数据范围 \(N\le15\),考虑状压。

设 \(f[i][j]\) 表示在所有串中匹配到第 \(i\) 位字符且匹配状态为 \(j\) 的方案数。

以及 \(g[i][c]\) 表示在所有串中匹配至第 \(i\) 位字符且第 \(i\) 位字符为 \(c\) 的合法最大匹配数(状态的数值最大)

那么我们就可以开始愉快地 \(\text{DP}\) 啦。

参考代码:

/*--------------------------------
Code name: C.cpp
Author: The Ace Bee
This code is made by The Ace Bee
--------------------------------*/
#include <cstdio>
#include <cstring>
#define rg register
#define file(x) \
freopen(x".in", "r", stdin); \
freopen(x".out", "w", stdout);
inline int read() {
int s = 0; bool f = false; char c = getchar();
while (c < '0' || c > '9') f |= (c == '-'), c = getchar();
while (c >= '0' && c <= '9') s = (s << 3) + (s << 1) + (c ^ 48), c = getchar();
return f ? -s : s;
}
inline int count(int x) {
int res = 0;
while (x) res += x & 1, x >>= 1;
return res;
}
char s[17][55];
int f[55][1 << 15 | 10], g[55][30];
inline void plus(int& a, int b) { a = (a + b) % 1000003; }
int main() {
// file("C");
for (rg int T = read(); T; --T) {
memset(f, 0, sizeof f);
memset(g, 0, sizeof g);
int n = read(), k = read();
for (rg int i = 1; i <= n; ++i) scanf("%s", s[i]);
int len = strlen(s[1]);
for (rg int i = 0; i < len; ++i)
for (rg char c = 'a'; c <= 'z'; ++c)
for (rg int j = 1; j <= n; ++j)
if (s[j][i] == '?' || s[j][i] == c)
g[i][c - 'a'] |= 1 << (j - 1);
int lmt = (1 << n) - 1;
f[0][lmt] = 1;
for (rg int i = 0; i < len; ++i)
for (rg int j = 0; j <= lmt; ++j)
if (f[i][j])
for (rg char c = 'a'; c <= 'z'; ++c)
plus(f[i + 1][g[i][c - 'a'] & j], f[i][j]);
int ans = 0;
for (rg int i = 0; i <= lmt; ++i)
if (count(i) == k) plus(ans, f[len][i]);
printf("%d\n", ans);
}
return 0;
}

「SDOI2009」Bill的挑战的更多相关文章

  1. 「SDOI2009」HH的项链

    「SDOI2009」HH的项链 传送门 数据加强了,莫队跑不过了. 考虑用树状数组. 先把询问按右端点递增排序. 然后对于每一种贝壳,我们都用它最右一次出现的位置计算答案. 具体细节看代码吧. 参考代 ...

  2. 【BZOJ1879】【SDOI2009】Bill的挑战 [状压DP]

    Bill的挑战 Time Limit: 4 Sec  Memory Limit: 64 MB[Submit][Status][Discuss] Description Input 第一行:一个整数T, ...

  3. 【SDOI2009】Bill的挑战

    Description Sheng bill不仅有惊人的心算能力,还可以轻松地完成各种统计.在昨天的比赛中,你凭借优秀的程序与他打成了平局,这导致Sheng bill极度的不满.于是他再次挑战你.这次 ...

  4. 「SDOI2009」虔诚的墓主人

    传送门 Luogu 解题思路 离散化没什么好说 有一种暴力的想法就是枚举每一个坟墓,用一些数据结构维护一下这个店向左,向右,向上,向下的常青树的个数,然后用组合数统计方案. 但是网格图边长就有 \(1 ...

  5. 【BZOJ1879】[SDOI2009]Bill的挑战(动态规划)

    [BZOJ1879][SDOI2009]Bill的挑战(动态规划) 题面 BZOJ 洛谷 题解 本来还想着容斥来着,这个数据范围直接暴力就好.设\(f[i][S]\)表示当前填到了第\(i\)位,和\ ...

  6. bzoj千题计划207:bzoj1879: [Sdoi2009]Bill的挑战

    http://www.lydsy.com/JudgeOnline/problem.php?id=1879 f[i][j] 表示匹配了i个字符,匹配字符串的状态为j的方案数 枚举下一个字符是什么 计算加 ...

  7. 「WC2016」挑战NPC

    「WC2016」挑战NPC 解题思路 这个题建图非常厉害,带花树什么的只会口胡根本写不动,所以我写了机房某大佬教我的乱搞. 考虑把一个筐 \(x\) 拆成 \(x1,x2,x3\) 三个点,且这三个点 ...

  8. 【BZOJ1879】[Sdoi2009]Bill的挑战 状压DP

    [BZOJ1879][Sdoi2009]Bill的挑战 Description Input 本题包含多组数据.  第一行:一个整数T,表示数据的个数.  对于每组数据:  第一行:两个整数,N和K(含 ...

  9. bzoj 1879: [Sdoi2009]Bill的挑战

    题目链接 bzoj 1879: [Sdoi2009]Bill的挑战 题解 n<=15,装压吧 对所有字符串进行装压 可以预处理一个数组can[i][j]表示所有的字符串中,有哪些可以在第i位匹配 ...

随机推荐

  1. SQL 杂项

    select  *  from 表  where to_date(ksrq,'yyyy-MM-dd')<=sysdate and  sysdate  <= to_date(jsrq,'yy ...

  2. 链剖-进阶ing-填坑-NOIP2013-货车运输

    This article is made by Jason-Cow.Welcome to reprint.But please post the writer's address. http://ww ...

  3. Java 动态代理实现

    1.依赖 java.lang.reflect.Proxy - 提供了静态方法去创建动态代理类的实例: Interface InvocationHandler - 一个代理实例调用处理程序实现的接口 2 ...

  4. input的number类型只能输入正数,禁止负数输入

    <input type="number" max="10" min='1' id='number'> <script> document ...

  5. 多种语言输出helloworld

  6. blog主题——樱花

    贮存一下,blog代码 QAQ 页脚html <!--live2d--> <script src="https://blog-static.cnblogs.com/file ...

  7. docker中安装mysql遇到的问题

    在虚拟机上用docker安装了一个mysql最新版,然后在本地用sqlyog区连接报错‘plugin  cachin_sha2_password  could  not  be  loaded’经过上 ...

  8. CentOS6.5_x64卸载系统原有MySQL

    1.查看系统是否存在MySQL的版本 rpm -qa | grep mysql 2.删除老版本的开头文件和库(rpm -e --nodeps XXX) rpm -e --nodeps mysql-5. ...

  9. springboot去除内嵌tomcat和打包在tomcat中运行需要做的步骤

    去除内嵌tomcat和添加jsp依赖 去除内嵌tomcat 在springboot启动依赖中去除内嵌tomcat <dependency> <groupId>org.sprin ...

  10. reStructuredText语法

    reStructuredText 除了makedown语法这还存在另一种语法reStructuredText 相对Markdown来说,在写书方面更有优势: 使用sphnix能够自动生成目录和索引文件 ...