2017多校赛 1002 Rikka with String(AC自动机+状压)
来源:互联网 发布:义乌淘宝模特招聘 编辑:程序博客网 时间:2024/06/08 08:27
这个题目和之前做的一道题目很像:类似的题目
对于题目要求枚举到2L长度的串,由于01串本身的性质我们只用枚举到L就行了,那一半是对称的。在枚举这一半时是可以随便枚举的,因为这一般确定了,那么另一半也确定了,这样2L的串一定是个01串。
然后对于输入的n个串做如下处理:对于每个输入的串讲其插入AC自动机里面,然后将每个串的反串(然后01颠倒)也插入进去。比如对于001,我们不仅要插入001,还要将011插入,因为当在前L中出现011,由于01串的性质在,L到2L中一定会出现001。
同时还有特殊情况要额外处理,对于001来说,如果前面L的串中没有出现001,但是在L的最后两位出现00了,根据01串的性质,在2L的串中一定会出先001,所以对每个输入的字符串中具有像00这样性质的串也要导入AC自动机中,但是要把状态压缩到AC自动机的另一个数组中,因为这是枚举到最后一位才会处理的,和之前的状态不能放在一起。
处理方法:
对于一个输入的串,我们去枚举串的每一个位置,在这个位置将串一分为2,如果这个串在对称范围内满足01串的性质,那么我们就把长的那个插入自动机中
做好以上工作后就,在自动机上跑一遍状压DP就可以了,和之前那道题目一样的做法。dp[i][j][k],表示当枚举到i这个位置时,位于AC自动机的j节点上,当前的状态压缩在k上。枚举到最后一位别忘了特殊处理哦!
#include <stdio.h>#include <iostream>#include <cstring>#include <algorithm>#include <queue>using namespace std;#define Max_N 2000#define MOD 998244353struct Trie{ int next[Max_N][3], fail[Max_N], end1[Max_N],end2[Max_N]; int root, L; int newnode() { for (int i = 0; i < 2; i++) next[L][i] = -1; end1[L++] = 0; return L-1; } void init() { L = 0; root = newnode(); memset(end2, 0, sizeof(end2)); } void insert(char buf[], int x, int flag) { int len = strlen(buf); int now = root; for (int i = 0; i < len; i++) { if (next[now][buf[i]-'0'] == -1) next[now][buf[i]-'0'] = newnode(); now = next[now][buf[i] - '0']; } if (flag) end1[now] |= 1<<x; else end2[now] |= 1<<x; } void build() { queue<int> q; fail[root] = root; for (int i = 0; i < 2; 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(); end1[now] |= end1[fail[now]]; end2[now] |= end2[fail[now]]; for (int i = 0; i < 2; 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]); } } }};Trie ac;char str[30];char str1[30];char str2[30];int dp[110][2000][1<<6];int main(){ int T; cin >> T; int n, l; while (T--) { scanf("%d%d", &n, &l); ac.init(); for (int i = 0; i < n; i++) { scanf("%s", str); ac.insert(str,i, 1); int len1 = strlen(str); for (int j = 1; j < len1; j++) { memset(str1, '\0', sizeof(str1)); memset(str2, '\0', sizeof(str2));//str1是后半部分,str2是前半部分。 reverse_copy(str+j, str+len1, str1);//将str串的j到len1的部分反转存入str1中。 reverse(str1, str1+strlen(str1));//将str1串反转。 reverse_copy(str+0, str+j, str2); int len2 = min(strlen(str1), strlen(str2)); int flag = 1; for (int k = 0; k < len2; k++) { if (str1[k] == str2[k]) { flag = 0; break; } } if (!flag) continue; if (strlen(str1) > strlen(str2)) { reverse(str1, str1+strlen(str1)); for (int k = 0; k < strlen(str1); k++) if (str1[k] == '0') str1[k] = '1'; else str1[k] = '0'; ac.insert(str1, i, 0); } else { reverse(str2, str2+strlen(str2)); ac.insert(str2, i, 0); } } reverse(str, str+strlen(str)); for (int j = 0; j < strlen(str); j++) if (str[j] == '0') str[j] = '1'; else str[j] = '0'; ac.insert(str, i, 1); } ac.build(); memset(dp, 0, sizeof(dp)); dp[0][0][0] = 1; int m = l; for (int i = 0; i < m; i++) for (int j = 0; j < ac.L; j++) { for (int w = 0; w < (1<<n); w++) { if(dp[i][j][w] == 0) continue; for (int t = 0; t < 2; t++) { int next1 = ac.next[j][t]; if (i != m-1) dp[i+1][next1][w|ac.end1[next1]] += dp[i][j][w], dp[i+1][next1][w|ac.end1[next1]] %= MOD; else dp[i+1][next1][w|ac.end1[next1]|ac.end2[next1]] += dp[i][j][w],dp[i+1][next1][w|ac.end1[next1]|ac.end2[next1]] %= MOD;//当到L位时特殊处理。 } } } long long int sum = 0; for (int i = 0; i < ac.L; i++) { sum += dp[m][i][(1<<n)-1]; sum %= MOD; } cout << sum << endl; } return 0;}
阅读全文
0 0
- 2017多校赛 1002 Rikka with String(AC自动机+状压)
- HDU 6086 Rikka with String (AC 自动机+状压 dp, 2017 Multi-Univ Training Contest 5)
- hdu 6086 -- Rikka with String(AC自动机 + 状压DP)
- hdu 6086 Rikka with String ac自动机+dp
- Hdu-6086 Rikka with String(AC自动机+DP)
- HDU - 5202 - Rikka with string (DFS)
- hdu 5202 Rikka with string(模拟)
- Rikka with string-------(BestCoder Round #37)
- bestcoder Rikka with string
- Rikka with string
- HDU5202 Rikka with string
- hdu5202 Rikka with string
- Rikka with String HDU
- HDU 6096 String(AC自动机)
- hdoj-6096String(AC自动机)
- hdu6096 String AC自动机
- hdu6096 String【AC自动机】
- Rikka with string BC#37
- Mysql常用操作符
- 【字符串】【KMP模板--最小循环节总结】
- 五杆机构正运动学公式、逆运动学公式MATLAB代码
- C#对Sqlite数据库的操作类
- 二叉搜索树 (c++非递归版)
- 2017多校赛 1002 Rikka with String(AC自动机+状压)
- hibernate入门——基础
- Spring利用jdbcTemplate批量添加
- 前端系列“小”问题
- JavaScript 内置对象
- SpringMvc学习笔记(二)- SpringMvc的执行流程
- linux 安装es 及异常处理
- Hadoop 与Spark比较
- CSS3阴影 box-shadow的使用和技巧总结