Description

给定k个长度不超过L的01串,求有多少长度为n的01串S满足:

1.该串是回文串

2.该串不存在两个不重叠的子串,在给定的k个串中。

即不存在a<=b<c<=d,S[a,b]是k个串中的一个,S[c,d]是k个串中的一个

(It does not contain two non-overlapped substrings in the given list of K binary strings.)

举个例子,若给定2(k=2)个01串:101和1001

1010001和1010101均不满足条件。前者不满足条件1,后者不满足条件2

Input

第一行两个整数n,k

以下k行,每行一个01串

Output

输出一个整数表示答案,答案mod 1000000007(10^9+7)
对原串和反串分别建ac自动机,从两边向中间处理回文串,同时在自动机上走,若匹配到一个串就记录并回到起点
dp状态表示为 当前已确定前缀、后缀在ac自动机上的位置,以及是否已匹配到一个串 的方案数
最后检查是否在跨过中点的位置匹配到一个串
#include<cstdio>
#include<cstring>
#include<algorithm>
const int P=1e9+;
int n,k,ls[];
char s[][];
inline void inc(int&a,int b){
b+=a-P;
a=b+(b>>&P);
}
struct acam{
int ch[][],fa[];
int S[][];
bool e[];
int ptr;
acam(){
memset(this,,sizeof(acam));
ptr=;
ch[][]=ch[][]=;
}
void ins(char*s,int a){
int w=;
for(int i=;s[i];++i){
int c=s[i]-'',&u=ch[w][c];
if(!u)u=++ptr;
w=u;
S[w][a]|=<<i;
}
e[w]=;
}
void ins(char*s,int a,int len){
int w=;
for(int i=len-;i>=;--i){
int c=s[i]-'',&u=ch[w][c];
if(!u)u=++ptr;
w=u;
S[w][a]|=<<i;
}
e[w]=;
}
void build(){
int q[],ql=,qr=;
q[++qr]=;
while(ql!=qr){
int w=q[++ql];
for(int i=;i<;++i){
int&u=ch[w][i];
(u?fa[q[++qr]=u]:u)=ch[fa[w]][i];
}
}
for(int i=;i<=qr;++i){
int w=q[i],f=fa[w];
e[w]|=e[f];
for(int j=;j<k;++j)S[w][j]|=S[f][j];
}
}
}t1,t2;
int f[][][][];
int main(){
scanf("%d%d",&n,&k);
for(int i=;i<k;++i){
scanf("%s",s[i]);
ls[i]=strlen(s[i]);
t1.ins(s[i],i);
t2.ins(s[i],i,ls[i]);
}
t1.build();
t2.build();
int now=;
f[][][][]=;
for(int t=;t<n/;++t,now^=){
memset(f[now^],,sizeof(f[][])*(t1.ptr+));
for(int c=;c<;++c){
for(int a=;a<=t1.ptr;++a)if(!t1.e[a]){
int a1=t1.ch[a][c],is=;
if(t1.e[a1])a1=,is=;
int(*f1a)[]=f[now^][a1];
int(*f0a)[]=f[now][a];
for(int b=;b<=t2.ptr;++b)if(!t2.e[b]){
int b1=t2.ch[b][c];
int v=is;
if(t2.e[b1])b1=,++v;
for(int x=;x+v<;++x){
inc(f1a[b1][x+v],f0a[b][x]);
}
}
}
}
}
if(n&){
memset(f[now^],,sizeof(f[][])*(t1.ptr+));
for(int c=;c<;++c){
for(int a=;a<=t1.ptr;++a)if(!t1.e[a]){
int a1=t1.ch[a][c],is=;
if(t1.e[a1])a1=,is=;
int(*f1a)[]=f[now^][a1];
int(*f0a)[]=f[now][a];
for(int b=;b<=t2.ptr;++b)if(!t2.e[b]){
int v=is;
for(int x=;x+v<;++x){
inc(f1a[b][x+v],f0a[b][x]);
}
}
}
}
now^=;
}
int ans=;
for(int a=;a<=t1.ptr;++a){
for(int b=;b<=t2.ptr;++b){
inc(ans,f[now][a][b][]);
for(int c=;c<k;++c)if(t1.S[a][c]<<&t2.S[b][c])goto o;
inc(ans,f[now][a][b][]);
o:;
}
}
printf("%d\n",ans);
return ;
}

bzoj 3768: spoj 4660 Binary palindrome二进制回文串的更多相关文章

  1. [LeetCode] Shortest Palindrome 最短回文串

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  2. lintcode :Valid Palindrome 有效回文串

    题目: 有效回文串 给定一个字符串,判断其是否为一个回文串.只包含字母和数字,忽略大小写. 样例 "A man, a plan, a canal: Panama" 是一个回文. & ...

  3. BZOJ.2565.[国家集训队]最长双回文串(Manacher/回文树)

    BZOJ 洛谷 求给定串的最长双回文串. \(n\leq10^5\). Manacher: 记\(R_i\)表示以\(i\)位置为结尾的最长回文串长度,\(L_i\)表示以\(i\)开头的最长回文串长 ...

  4. [leetcode]125. Valid Palindrome判断回文串

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  5. [LeetCode] 214. Shortest Palindrome 最短回文串

    Given a string s, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  6. poj 3280 Cheapest Palindrome ---(DP 回文串)

    题目链接:http://poj.org/problem?id=3280 思路: dp[i][j] :=第i个字符到第j个字符之间形成回文串的最小费用. dp[i][j]=min(dp[i+1][j]+ ...

  7. poj3280 Cheapest Palindrome(回文串区间dp)

    https://vjudge.net/problem/POJ-3280 猛刷简单dp第一天第三题. 这个据说是[求字符串通过增减操作变成回文串的最小改动次数]的变体. 首先增减操作的实质是一样的,所以 ...

  8. Atcoder CODE FESTIVAL 2017 qual C D - Yet Another Palindrome Partitioning 回文串划分

    题目链接 题意 给定一个字符串(长度\(\leq 2e5\)),将其划分成尽量少的段,使得每段内重新排列后可以成为一个回文串. 题解 分析 每段内重新排列后是一个回文串\(\rightarrow\)该 ...

  9. 214 Shortest Palindrome 最短回文串

    给一个字符串 S, 你可以通过在字符串前面添加字符将其转换为回文串.找到并返回可以用这种方式转换的最短回文串.例如:给出 "aacecaaa",返回 "aaacecaaa ...

随机推荐

  1. MatchText MatchStr 区别

    区别就是 是否区分大小写. str=字符串,区分 text=文本,不区分 时间长了就忘了. function AnsiCompareText(const S1, S2: string): Intege ...

  2. L1-021 重要的话说三遍

    这道超级简单的题目没有任何输入. 你只需要把这句很重要的话 —— “I'm gonna WIN!”——连续输出三遍就可以了. 注意每遍占一行,除了每行的回车不能有任何多余字符. 输入样例: 无 输出样 ...

  3. 【DevExpress v17.2新功能预告】增强ASP.NET TreeList

    本文主要为大家介绍在下一个主要版本v17.2中,DevExpress ASP.NET TreeList获得的一些重大改进.DevExpress ASP.NET TreeList和GridView控件在 ...

  4. 把腾讯云的ubuntu16.04升级到18.04

    腾讯云买的服务器也没怎么弄,正好重装一下玩乐了. 1. 重装系统,在腾讯云里先停机,然后重装系统,目前最高是ubuntu16.04.为什么选择Ubuntu?因为,因为习惯吧,之前学习laravel就是 ...

  5. Python高手之路 ------读书有感

    最近忙中偷闲把前些年买的<Python高手之路>翻了出来,大致看完了一遍,其中很多内容并不理解,究其原因应该是实践中的经验不足,而这对于现如今的我仍是难以克服的事情,对此也就只能说是看会了 ...

  6. Unity 3D的常用快捷键

    Unity中的常用快捷键 本文提供全流程,中文翻译.Chinar坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) 1 Windows系统Unity3 ...

  7. linux中文件上传下载

    windows篇 linux文件下载到windows sz命令 登录到linux服务器使用 sz log.log 命令,弹出对话框选择下载文件的目录,点击确定即可. windows文件上传到linux ...

  8. python去掉字符串'\xa0'

    AssertionError: '5\xa0e\xa0*\xa0*\xa0*\xa05' != '5e***5'mystr = '5\xa0e\xa0*\xa0*\xa0*\xa05'mystr = ...

  9. StyleCop 是什么,可以帮助团队带来什么价值?

    StyleCop 本质上是一个 C# 源代码规则分析器,可以帮助团队成员强制执行一组代码样式和一致性规则. 本文将简述 StyleCop 以及它能为团队带来的价值. 本文内容 StyleCop 是什么 ...

  10. Codeforces Beta Round #81 A Transmigration

    在魔界战记中有一个设定叫做转生,当一个人物转生时,会保留之前的技能,但是技能等级需要乘以一个系数 k ,如果技能等级小于100,将会在转生之后失去该技能. 转生之后,会学到一些新技能.这些新技能附加的 ...