给你m个01串,问你有多少个长度为2L的01串,满足前半段倒置取反后等于后半段,并且包含所有的m个01串。

考虑单词完全在中线前面或者后面的情况,直接将单词及其倒置取反插入AC自动机,AC自动机每个结点用个tag压位记录单词集合。

对于跨越中线的情况,比如说110010是一个单词,枚举一个中线,

11 | 0010

在前面添加上10,变成10 11 | 0010,

那么其前半部分1011也是一个合法的单词,只不过这种类型的单词只有在dp到长度恰好为L时才能用。

于是AC自动机上每个结点记录两种tag,tag1是原始的单词集合(还有将它倒置再取反的单词),tag2是跨越中线的单词。

f(i,j,S)表示长度为i,到第j个结点,当前集合为S的方案数。第一维滚动。i枚举到n-1时,要用tag2,否则只用tag1。
#include<cstdio>
#include<queue>
#include<string>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
queue<int>q;
#define MOD 998244353
int child[2645][2],fail[2645],size,f[2][2645][64],tag[2645],tag2[2645];
void Insert(string S,int id)
{
int len=S.length();
int now=0;
for(int i=0;i<len;++i)
{
if(!child[now][S[i]-'0'])
child[now][S[i]-'0']=size++;
now=child[now][S[i]-'0'];
}
tag[now]|=(1<<id);
}
void Inser2(string S,int id){
int len=S.length();
int now=0;
for(int i=0;i<len;++i)
{
if(!child[now][S[i]-'0'])
child[now][S[i]-'0']=size++;
now=child[now][S[i]-'0'];
}
tag2[now]|=(1<<id);
}
void build()
{
fail[0]=-1;
q.push(0);
while(!q.empty())
{
int U=q.front(); q.pop();
for(int i=0;i<2;++i)
if(child[U][i])
{
int V=fail[U];
while(V!=-1)
{
if(child[V][i])
{
fail[child[U][i]]=child[V][i];
break;
}
V=fail[V];
}
if(V==-1)
fail[child[U][i]]=0;
tag[child[U][i]]|=tag[fail[child[U][i]]];
tag2[child[U][i]]|=tag2[fail[child[U][i]]];
q.push(child[U][i]);
}
else if(U)
child[U][i]=child[fail[U]][i];
}
}
void Init()
{
memset(child,0,sizeof(child));
memset(fail,0,sizeof(fail));
memset(tag,0,sizeof(tag));
memset(tag2,0,sizeof(tag2));
size=1;
}
int n,m,T;
int main()
{
// freopen("b.in","r",stdin);
scanf("%d",&T);
string s;
for(;T;--T){
scanf("%d%d",&m,&n);
Init();
for(int i=0;i<m;++i){
cin>>s;
int len=s.length();
// cout<<"::"<<s<<endl;
Insert(s,i);
string t=s;
reverse(t.begin(),t.end());
for(int j=0;j<len;++j){
t[j]=((t[j]-'0')^1)+'0';
}
// cout<<"::"<<t<<endl;
Insert(t,i);
for(int j=0;j<min(len,n);++j){
bool flag=1;
for(int k=j+1,l=j;k<s.length() && l>=0;++k,--l){
if((s[k]-'0')^(s[l]-'0')!=1){
flag=0;
break;
}
}
if(!flag){
continue;
}
t=s.substr(0,j+1);
for(int k=(j+1)*2;k<s.length();++k){
t=(char)(((s[k]-'0')^1)+'0')+t;
}
// cout<<":"<<t<<endl;
Inser2(t,i);
}
}
build();
memset(f,0,sizeof(f));
f[0][0][0]=1;
bool cur=0;
for(int i=0;i<n;++i){
memset(f[cur^1],0,sizeof(f[cur^1]));
for(int j=0;j<size;++j)
for(int k=0;k<(1<<m);++k){
if(!f[cur][j][k])
continue;
if(i<n-1)
for(int l=0;l<2;++l)
f[cur^1][child[j][l]][k|tag[child[j][l]]]=
(f[cur^1][child[j][l]][k|tag[child[j][l]]]+f[cur][j][k])%MOD;
else{
for(int l=0;l<2;++l)
f[cur^1][child[j][l]][k|tag[child[j][l]]|tag2[child[j][l]]]=
(f[cur^1][child[j][l]][k|tag[child[j][l]]|tag2[child[j][l]]]+f[cur][j][k])%MOD;
}
}
cur^=1;
}
int ans=0;
for(int j=0;j<size;++j)
ans=(ans+f[cur][j][(1<<m)-1])%MOD;
printf("%d\n",ans);
}
return 0;
}

【AC自动机】【状压dp】【滚动数组】hdu6086 Rikka with String的更多相关文章

  1. zoj3545Rescue the Rabbit (AC自动机+状压dp+滚动数组)

    Time Limit: 10 Seconds      Memory Limit: 65536 KB Dr. X is a biologist, who likes rabbits very much ...

  2. hdu 2825 aC自动机+状压dp

    Wireless Password Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  3. hdu 4057--Rescue the Rabbit(AC自动机+状压DP)

    题目链接 Problem Description Dr. X is a biologist, who likes rabbits very much and can do everything for ...

  4. BZOJ1559 [JSOI2009]密码 【AC自动机 + 状压dp】

    题目链接 BZOJ1559 题解 考虑到这是一个包含子串的问题,而且子串非常少,我们考虑\(AC\)自动机上的状压\(dp\) 设\(f[i][j][s]\)表示长度为\(i\)的串,匹配到了\(AC ...

  5. HDU 3247 Resource Archiver(AC自动机 + 状压DP + bfs预处理)题解

    题意:目标串n( <= 10)个,病毒串m( < 1000)个,问包含所有目标串无病毒串的最小长度 思路:貌似是个简单的状压DP + AC自动机,但是发现dp[1 << n][ ...

  6. hdu2825 Wireless Password(AC自动机+状压dp)

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission ...

  7. hdu 6086 -- Rikka with String(AC自动机 + 状压DP)

    题目链接 Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, s ...

  8. UVALive - 4126 Password Suspects (AC自动机+状压dp)

    给你m个字符串,让你构造一个字符串,包含所有的m个子串,问有多少种构造方法.如果答案不超过42,则按字典序输出所有可行解. 由于m很小,所以可以考虑状压. 首先对全部m个子串构造出AC自动机,每个节点 ...

  9. 【hdu2825】ac自动机 + 状压dp

    传送门 题目大意: 给你一些密码片段字符串,让你求长度为n,且至少包含k个不同密码片段串的字符串的数量. 题解: 因为密码串不多,可以考虑状态压缩 设dp[i][j][sta]表示长为i的字符串匹配到 ...

  10. HDU 4057:Rescue the Rabbit(AC自动机+状压DP)***

    http://acm.hdu.edu.cn/showproblem.php?pid=4057 题意:给出n个子串,串只包含‘A’,'C','G','T'四种字符,你现在需要构造出一个长度为l的串,如果 ...

随机推荐

  1. POJ 3061 Subsequence ( 尺取法)

    题目链接 Description A sequence of N positive integers (10 < N < 100 000), each of them less than ...

  2. elementui table 多选 获取id

    //多选相关方法 toggleSelection(rows) { if (rows) { rows.forEach(row => { this.$refs.multipleTable.toggl ...

  3. Python ctypes 在 Python 2 和 Python 3 中的不同 // 使用ctypes过程中问题汇总

    In Python 2.7, strings are byte-strings by default. In Python 3.x, they are unicode by default. Try ...

  4. linux下暴力破解工具hydra【转】

    一.简介 Number one of the biggest security holes are passwords, as every password security study shows. ...

  5. python中的argparse模块

    argparse干什么用的? 答:参数设置,比如python demo.py -h 诸如此类的. 开始学习这个模块: parser = argparse.ArgumentParser() #使用这个模 ...

  6. linux下pthread_cancel无法取消线程的原因【转】

    转自:http://blog.csdn.net/huangshanchun/article/details/47420961 版权声明:欢迎转载,如有不足之处,恳请斧正. 一个线程可以调用pthrea ...

  7. glob模块的使用

    glob模块 功能描述:glob模块可以使用Unix shell风格的通配符匹配符合特定格式的文件和文件夹,跟windows的文件搜索功能差不多.glob模块并非调用一个子shell实现搜索功能,而是 ...

  8. c++ ui 库

    Dulib 比较流行的direct ui 界面框架 UIStone 据说金山词霸用着,查询资料甚少 DirectUI qq使用了据说,多学习学习吧 基于directUI的dulib不错 c盘没空间,运 ...

  9. scrapy抓取学院新闻报告

    抓取四川大学公共管理学院官网(http://ggglxy.scu.edu.cn)所有的新闻咨询. 实验流程 1.确定抓取目标.2.制定抓取规则.3.'编写/调试'抓取规则.4.获得抓取数据 1.确定抓 ...

  10. C++ 模板的用法

    C++中的高阶手法就会用到泛型编程,主要有函数模板, 在程序中使用模板的好处就是在定义时不需要指定具体的参数类型,而在使用时确可以匹配其它任意类型, 定义格式如下 template <class ...