【AC自动机】【状压dp】【滚动数组】hdu6086 Rikka with String
给你m个01串,问你有多少个长度为2L的01串,满足前半段倒置取反后等于后半段,并且包含所有的m个01串。
考虑单词完全在中线前面或者后面的情况,直接将单词及其倒置取反插入AC自动机,AC自动机每个结点用个tag压位记录单词集合。
对于跨越中线的情况,比如说110010是一个单词,枚举一个中线,
11 | 0010
在前面添加上10,变成10 11 | 0010,
那么其前半部分1011也是一个合法的单词,只不过这种类型的单词只有在dp到长度恰好为L时才能用。
于是AC自动机上每个结点记录两种tag,tag1是原始的单词集合(还有将它倒置再取反的单词),tag2是跨越中线的单词。
#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的更多相关文章
- zoj3545Rescue the Rabbit (AC自动机+状压dp+滚动数组)
Time Limit: 10 Seconds Memory Limit: 65536 KB Dr. X is a biologist, who likes rabbits very much ...
- hdu 2825 aC自动机+状压dp
Wireless Password Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- hdu 4057--Rescue the Rabbit(AC自动机+状压DP)
题目链接 Problem Description Dr. X is a biologist, who likes rabbits very much and can do everything for ...
- BZOJ1559 [JSOI2009]密码 【AC自动机 + 状压dp】
题目链接 BZOJ1559 题解 考虑到这是一个包含子串的问题,而且子串非常少,我们考虑\(AC\)自动机上的状压\(dp\) 设\(f[i][j][s]\)表示长度为\(i\)的串,匹配到了\(AC ...
- HDU 3247 Resource Archiver(AC自动机 + 状压DP + bfs预处理)题解
题意:目标串n( <= 10)个,病毒串m( < 1000)个,问包含所有目标串无病毒串的最小长度 思路:貌似是个简单的状压DP + AC自动机,但是发现dp[1 << n][ ...
- hdu2825 Wireless Password(AC自动机+状压dp)
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission ...
- hdu 6086 -- Rikka with String(AC自动机 + 状压DP)
题目链接 Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, s ...
- UVALive - 4126 Password Suspects (AC自动机+状压dp)
给你m个字符串,让你构造一个字符串,包含所有的m个子串,问有多少种构造方法.如果答案不超过42,则按字典序输出所有可行解. 由于m很小,所以可以考虑状压. 首先对全部m个子串构造出AC自动机,每个节点 ...
- 【hdu2825】ac自动机 + 状压dp
传送门 题目大意: 给你一些密码片段字符串,让你求长度为n,且至少包含k个不同密码片段串的字符串的数量. 题解: 因为密码串不多,可以考虑状态压缩 设dp[i][j][sta]表示长为i的字符串匹配到 ...
- HDU 4057:Rescue the Rabbit(AC自动机+状压DP)***
http://acm.hdu.edu.cn/showproblem.php?pid=4057 题意:给出n个子串,串只包含‘A’,'C','G','T'四种字符,你现在需要构造出一个长度为l的串,如果 ...
随机推荐
- python初步学习-python控制流
语句书写规范 缩进在python语言书写中非常重要,如果缩进不规范,执行程序将会报错 引用维基百科中的叙述: Python開發者有意讓違反了縮排規則的程序不能通過編譯,以此來強迫程序員養成良好的編程習 ...
- Android--hardwareAccelerated 硬件加速详解 android:largeHeap="true"
做项目时,引导页面的ViewPager报了OOM异常,图片并不大,在清单文件Application节点中添加了两行代码就解决了这个问题 android:hardwareAccelerated=&quo ...
- c语言中的size_t
size_t unsigned int 类型,无符号,它的取值没有负数.用来表示 参数/数组元素个数,sizeof 返回值,或 str相关函数返回的 size 或 长度.sizeof 操作符的结果类型 ...
- FineReport——自定义控件实现填报提交事件和校验
在报表内部或者在引用报表的HTML页面,定义一个按钮标签,通过FR提供的方法实现提交功能. <button onclick="_g('${sessionID}').writeRepor ...
- 【Android开发日记】之入门篇(十五)——ViewPager+自定义无限ViewPager
ViewPager 在 Android 控件中,ViewPager 一直算是使用率比较高的控件,包括首页的banner,tab页的切换都能见到ViewPager的身影. viewpager 来源自 v ...
- linux命令(32):free命令
1.显示内存使用情况:free free –g free –m 2.以总和的形式显示内存的使用信息: free -t 3.周期性的查询内存使用信息:free -s 10
- linux命令(25):ln命令
命令格式: ln [参数][源文件或目录][目标文件或目录] 必要参数: -b 删除,覆盖以前建立的链接 -d 允许超级用户制作目录的硬链接 -f 强制执行 -i 交互模式,文件存在则提示用户是否覆盖 ...
- 机器学习方法(八):随机采样方法整理(MCMC、Gibbs Sampling等)
转载请注明出处:Bin的专栏,http://blog.csdn.net/xbinworld 本文是对参考资料中多篇关于sampling的内容进行总结+搬运,方便以后自己翻阅.其实参考资料中的资料写的比 ...
- Guid is not updated for cluster with specified cluster id; need to wait for hosts in this cluster to come up
http://mail-archives.apache.org/mod_mbox/cloudstack-users/201306.mbox/%3c201306181058330006472@gmail ...
- es6解构赋值总结
数组的解构赋值 1.简单的赋值方式 2.多维数组解构赋值 3.默认值,只有当右边对应位置为undefined时候才会选择默认(null不属于undefined) 4.左右不对等,会相应的对号入座,没有 ...