bzoj 3768: spoj 4660 Binary palindrome二进制回文串
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
#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二进制回文串的更多相关文章
- [LeetCode] Shortest Palindrome 最短回文串
Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...
- lintcode :Valid Palindrome 有效回文串
题目: 有效回文串 给定一个字符串,判断其是否为一个回文串.只包含字母和数字,忽略大小写. 样例 "A man, a plan, a canal: Panama" 是一个回文. & ...
- BZOJ.2565.[国家集训队]最长双回文串(Manacher/回文树)
BZOJ 洛谷 求给定串的最长双回文串. \(n\leq10^5\). Manacher: 记\(R_i\)表示以\(i\)位置为结尾的最长回文串长度,\(L_i\)表示以\(i\)开头的最长回文串长 ...
- [leetcode]125. Valid Palindrome判断回文串
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- [LeetCode] 214. Shortest Palindrome 最短回文串
Given a string s, you are allowed to convert it to a palindrome by adding characters in front of it. ...
- poj 3280 Cheapest Palindrome ---(DP 回文串)
题目链接:http://poj.org/problem?id=3280 思路: dp[i][j] :=第i个字符到第j个字符之间形成回文串的最小费用. dp[i][j]=min(dp[i+1][j]+ ...
- poj3280 Cheapest Palindrome(回文串区间dp)
https://vjudge.net/problem/POJ-3280 猛刷简单dp第一天第三题. 这个据说是[求字符串通过增减操作变成回文串的最小改动次数]的变体. 首先增减操作的实质是一样的,所以 ...
- Atcoder CODE FESTIVAL 2017 qual C D - Yet Another Palindrome Partitioning 回文串划分
题目链接 题意 给定一个字符串(长度\(\leq 2e5\)),将其划分成尽量少的段,使得每段内重新排列后可以成为一个回文串. 题解 分析 每段内重新排列后是一个回文串\(\rightarrow\)该 ...
- 214 Shortest Palindrome 最短回文串
给一个字符串 S, 你可以通过在字符串前面添加字符将其转换为回文串.找到并返回可以用这种方式转换的最短回文串.例如:给出 "aacecaaa",返回 "aaacecaaa ...
随机推荐
- python运维01-获取系统基础信息
1.获取系统主机名,IP,MAC地址 import socket import uuid macs = uuid.UUID(int = uuid.getnode()).hex[-12:] mac = ...
- 【DevExpress v17.2新功能预告】增强ASP.NET GridView的功能
在下一个主要版本v17.2中,我们将为DevExpress ASP.NET GridView添加一些优秀的新功能.在本文中为大家介绍的所有功能都可用于 GridView的ASP.NET WebForm ...
- Sysinternals Suite 2014.08.18
Windows Sysinternals Suite 是一套由微软官方免费提供的系统工具集,其中包含了大量超级实的优秀绿色小软件,譬如 Desktops (虚拟桌面).Process Explorer ...
- NodeJS 难点(网络,文件)的 核心 stream 一:Buffer
stream应用一图片转存服务 stream github教程 文件操作和网络都依赖了一个很重要的对象—— Stream, 而这个 <node深入浅出> 没有分析的, 所以读完这本书, 在 ...
- OC基础:内存(内存管理) 分类: ios学习 OC 2015-06-25 16:50 73人阅读 评论(0) 收藏
自动释放池: @autoreleasepool { } 内存管理机制 谁污染,谁治理 垃圾回收机制:gc(Garbage collection),由系统管理内存,开发人员不需要管理. OC ...
- HDU 2853 && HDU 3315
http://acm.hdu.edu.cn/showproblem.php?pid=2853 题意:给一个n-m二分图,边权用一个n*m的矩阵表示,给出初始匹配,求二分图完美匹配相比初始匹配改变了几条 ...
- 玩转TypeScript(2) --简单TypeScript类型
通过TypeScript的Module和Class,TypeScript提供了相对于javaScript更加清晰的代码构造,相较于javaScript的.js满天飞的代码,用TypeScript,你可 ...
- ListBox item Sort
将Rss内容读取到Listbox control中, 然后实现按照标题或发布日期进行排序. private void ListItemSort(string type) { if (type == & ...
- sparksql与hive整合
参考文献: http://blog.csdn.net/stark_summer/article/details/48443147 hive配置 编辑 $HIVE_HOME/conf/Hive-site ...
- android_serialport_api hacking
/************************************************************************************ * * android_se ...