题目链接

Problem Description
As we know, Rikka is poor at math. Yuta is worrying about this situation, so he gives Rikka some math tasks to practice. There is one of them:

Yuta has n 01 strings si, and he wants to know the number of 01 antisymmetric strings of length 2L which contain all given strings si as continuous substrings.

A 01 string s is antisymmetric if and only if s[i]≠s[|s|−i+1] for all i∈[1,|s|].

It is too difficult for Rikka. Can you help her?

In the second sample, the strings which satisfy all the restrictions are 000111,001011,011001,100110.

 
Input
The first line contains a number t(1≤t≤5), the number of the testcases.

For each testcase, the first line contains two numbers n,L(1≤n≤6,1≤L≤100).

Then n lines follow, each line contains a 01 string si(1≤|si|≤20).

 
Output
For each testcase, print a single line with a single number -- the answer modulo 998244353.
 
Sample Input
2
2 2
011
001
2 3
011
001
 
Sample Output
1
4
 
 
题意:反对称:对于一个长为2*N的串s[0~2*N-1],s[i]^s[2*N-1-i]=1 。现在有n个01串,求有多少个长为2*L的并且包含这n个串的 反对称01串?
 
思路:对于一个串包含在2*L的01串中,那么这个串要么在2*L的前半部分,要么后半部分,或者跨越中间,如果在后半部分,则需要找到其在前半部分的反对称01串,对于跨越中间的01串,则需要找到其在前面部分的串,例如:0 | 11,以竖线作为串中间,那么如果前面部分如果以00结束,那么一定含有 011这个串。把每个串的所有形式放入AC自动机对应的tire树中,然后状压递推。
 
代码如下:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
#include <string>
using namespace std;
const int mod=;
const int N=;
struct Node{
int id;
Node *fail;
Node *son[];
int tag1,tag2;
}node[N];
queue<Node *>q;
int tot;
int dp[][][]; void insert1(string s,int id)
{
int len=s.length();
Node *now=&node[];
for(int i=;i<len;i++)
{
int x=s[i]-'';
if(now->son[x]==NULL) now->son[x]=&node[tot++];
now=now->son[x];
}
now->tag1|=(<<id);
}
void insert2(string s,int id)
{
int len=s.length();
Node *now=&node[];
for(int i=;i<len;i++)
{
int x=s[i]-'';
if(now->son[x]==NULL) now->son[x]=&node[tot++];
now=now->son[x];
}
now->tag2|=(<<id);
}
void init()
{
for(int i=;i<N;i++)
{
node[i].id=i;
node[i].fail=NULL;
node[i].son[]=node[i].son[]=NULL;
node[i].tag1=node[i].tag2=;
}
}
void setFail()
{
Node* root=&node[];
q.push(root);
while(!q.empty())
{
Node* now=q.front(); q.pop();
for(int i=;i<;i++)
{
if(now->son[i])
{
Node* p=now->fail;
while(p && (!(p->son[i]))) p=p->fail;
now->son[i]->fail=(p)?(p->son[i]):(root);
now->son[i]->tag1|=now->son[i]->fail->tag1;
now->son[i]->tag2|=now->son[i]->fail->tag2;
q.push(now->son[i]);
}
else now->son[i]=(now!=root)?now->fail->son[i]:(root);
}
}
}
void print()
{
Node* now=&node[];
queue<Node*>qq;
qq.push(now);
while(!qq.empty())
{
now=qq.front(); qq.pop();
cout<<"Y:"<<now->id<<" ";
for(int i=;i<;i++)
{
if(now->son[i]) qq.push(now->son[i]),cout<<now->son[i]->id<<" ";
else cout<<"NULL"<<" ";
}
cout<<endl;
}
}
int main()
{
///cout << "Hello world!" << endl;
int t; cin>>t;
while(t--)
{
init();
tot=;
int n,L; scanf("%d%d",&n,&L);
for(int i=;i<n;i++)
{
string s; cin>>s;
insert1(s,i);
string t=s;
reverse(t.begin(),t.end());
int len=s.length();
for(int j=;j<len;j++)
t[j]=(char)((t[j]-'')^+'');
insert1(t,i); int mnLen=min(len,L);
for(int j=;j<mnLen;j++)
{
int f=;
for(int l=j,r=j+; l>=&&r<len; l--,r++)
{
if((s[l]^s[r])==) { f=; break; }
}
if(!f) continue;
t=s.substr(,j+);
for(int k=*j+;k<len;k++)
{
t=(char)((s[k]-'')^+'')+t;
}
insert2(t,i);
}
}
///print();
setFail();
memset(dp,,sizeof(dp));
dp[][][]=;
int cn=,stu=(<<n);
for(int i=;i<L;i++)
{
int c=cn^;
memset(dp[c],,sizeof(dp[c]));
for(int j=;j<tot;j++)
{
for(int s=;s<stu;s++)
{
if(!dp[cn][j][s]) continue;
if(i<L-)
for(int k=;k<;k++)
{
int x=node[j].son[k]->id;
int tag=node[x].tag1;
dp[c][x][s|tag]=(dp[c][x][s|tag]+dp[cn][j][s])%mod;
}
else
for(int k=;k<;k++)
{
int x=node[j].son[k]->id;
int tag=node[x].tag1|node[x].tag2;
dp[c][x][s|tag]=(dp[c][x][s|tag]+dp[cn][j][s])%mod;
}
}
}
cn=c;
}
int ans=;
for(int i=;i<tot;i++)
{
ans=(ans+dp[cn][i][stu-])%mod;
}
printf("%d\n",ans);
}
return ;
}

hdu 6086 -- Rikka with String(AC自动机 + 状压DP)的更多相关文章

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

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

  2. HDU 6086 Rikka with String AC自动机 + DP

    Rikka with String Problem Description As we know, Rikka is poor at math. Yuta is worrying about this ...

  3. HDU 2825 Wireless Password(AC自动机 + 状压DP)题解

    题意:m个密码串,问你长度为n的至少含有k个不同密码串的密码有几个 思路:状压一下,在build的时候处理fail的时候要用 | 把所有的后缀都加上. 代码: #include<cmath> ...

  4. HDU - 2825 Wireless Password (AC自动机+状压DP)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2825 题意:给一些字符串,构造出长度为n的字符串,它至少包含k个所给字符串,求能构造出的个数. 题解: ...

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. pyspider--post

    #!/usr/bin/env python# -*- encoding: utf-8 -*-# Created on 2018-08-19 14:47:28# Project: HBGGZY_SBJ ...

  2. 【笔记】计算机原理,网络,Linux操作系统

    一,计算机原理 1,化繁为简的思想,产生二进制,产生把所有计算归结为加法运算 二,网络 1,分层思想,产生ISO七层协议,商用了TCP/IP的五层协议 三,Linux操作系统 1,分层思想,硬件-&g ...

  3. Python学习积累:使用help();打印多个变量;fileno()

    1.使用篇: 1.1如何从help()退出: 直接回车即可! 2.技能篇: 2.1 如何一次性打印多个变量? 多个变量中间使用逗号隔开,且引用变量为%(变量1,变量2,变量3), 2.2fileno( ...

  4. face_recognition 模块安装

    https://blog.csdn.net/qq_15192373/article/details/78623741 https://blog.csdn.net/roguesir/article/de ...

  5. centos 7 一些命令

    su 切换到管理员账户cd 'wo shi mu lu'ls 查看 当前目录或者文件tar -xvzf pip-10.0.1.tar.gz 解压 文件systemctl restart network ...

  6. 小姐姐手把手教你JS数组中的对象去重

    有时候数据库中的数据重复的,我们另一个需求需要数据的唯一性 那么这时候就用到这个方法了  我还是以截图的方式发粗来  不然太丑了 见谅 console.log(map)打印出来的结果已经帮我们把需要的 ...

  7. Python下安装MySQLdb模块

    ----------------------[针对Windows下python 的MySQLdb模块安装]--------------------- 一.检查MySQLdb模块是否安装,可在DOS命令 ...

  8. [网络流]Farm Tour(费用流

    Farm Tour 题目描述 When FJ's friends visit him on the farm, he likes to show them around. His farm compr ...

  9. python_day10

    目录: 并发多线程 协程 I/O多路复用(未完成,待续) 一.并发多线程 1.线程简述: 一条流水线的执行过程是一个线程,一条流水线必须属于一个车间,一个车间的运行过程就是一个进程(一个进程内至少一个 ...

  10. ios 学习路线总结

    学习方法 面对有难度的功能,不要忙着拒绝,而是挑战一下,学习更多知识. 尽量独立解决问题,而不是在遇到问题的第一想法是找人. 多学习别人开源的第三方库,能够开源的库一定有值得学习的地方,多去看别的大神 ...