【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的串,如果 ...
随机推荐
- document的属性与方法小结
document节点是文档的根节点,每张网页都有自己的document节点.属性:1:document.doctype----它是一个对象,包含了当前文档类型 (Document Type Decla ...
- 制作Solaris系统的USB启动盘
制作方法: 1. wget http://192.168.2.5/surefiler-installer/2011-12-09/devel-2011.12.9.tgz 2. cd /root tar ...
- CART算法(转)
来源:http://www.cnblogs.com/pinard/p/6053344.html 作者:刘建平Pinard 对于C4.5算法,我们也提到了它的不足,比如模型是用较为复杂的熵来度量,使用了 ...
- ORA-02291:parent key not found
Hibernate operation: Could not execute JDBC batch update; SQL [insert into dchnpricecarchancesource ...
- Android 6.0 Marshmallow root 方法
android 6.0 已经推出 release 版本了, nexus 5,6,7,9 都放了官方镜像, 本篇文章使用 nexus 6 安装最新的 android 6.0 并进行root step 1 ...
- 【Android framework】AndroidManagerService初始化流程
源码基于Android 4.4. system_server的初始化 system_server受AMS管理,负责启动framework-res.apk和SettingsProvider.apk. ...
- Django 如何实现文件下载
1. 思路: 文件,让用户下载 - a标签+静态文件 - 设置响应头(django如何实现文件下载) 2. a标签实现 <a href="/static/xxx.xlsx"& ...
- Oracle 通用存储过程
CREATE OR REPLACE PROCEDURE P_Pub_GetList ( StrWhere varchar2, ---查询条件(不带where关键字的查询条件) TableName va ...
- grid+report 怎么在项目中使用
grid+report 的例子很丰富,首先看你所用对应编程语言的例子.参考帮助的“产品介绍->快速入门指导”部分.根据快速入门指导中的说明,先把例子程序运行. 例子分两部分:1.报表模板例子,主 ...
- Efficient Graph-Based Image Segmentation
转:http://blog.csdn.net/asongoficeandfire/article/details/8434799 Efficient Graph-Based Image Segment ...