n=40的01串,求有多少m=40的01串中包含它,包含的定义是存在子串有至多一个字符不相同

600组n=15的数据 15组n=40的数据,所以我们只能支持n^5的算法。

陷入两个比较有意思的坑:

1 如果手动构建fail树,建立两个并行的串,左串代表当前未使用那一个可以不相同的字符的名额,右串代表已经使用了这个名额,那么按照bfs m步的想法,左串可以通过“使用名额”到达右串,也可以通过“不使用名额”走一个fail,而右串一旦失配,只能在右串的对应位置进行fail。这样跑一遍矩阵乘法,复杂度是(2*n)^3

但是不对,因为假设右串失配了,可能距离一个点从左串跳过来已经过了很久了,那个名额已经可以再次使用了,所以它可以再跳回左串。这样就会导致我们失去一些答案。

2 如果插入n+1个串,代表原串,和原串第i个位置不同的串,这样点是n^3,那么矩阵乘法是n^6,还有一个logm的快速幂时间,复杂度会超

陷入了误区:快速幂一定比直接转移快。快速幂的快体现在将m转移到logm,但是二维矩阵的自乘是(点数^3)的,加一个快速幂的优化是从m(n*n)到logm(n*n*n)。

所以暴力插入n+1个串建树,暴力(n*n)^2的枚举边,跑m次转移。

L read() {L x;scanf("%lld" , &x) ; return x ; } ;

char s[50] ;

L c[40 * 40 + 5] ;
L a[40 * 40 + 5][40 * 40 + 5] ;
L b[40 * 40 + 5] ;
L n , m ; struct AC{
L next[40 * 40 + 5][2] , fail[40 * 40 + 5] , en[40 * 40 + 5] ;
L ro , to ;
L newnode() {
to++;
for(L i = 0 ; i < 2 ; i ++ ) next[to][i] = -1 ;
en[to] = 0 ;
return to ;
}
void init() {
to = -1 ;
ro = newnode() ;
}
void inse() {
L now = ro ;
for(L i = 1 ; i <= n ; i ++ ) {
if(next[now][s[i]-'0'] == -1) next[now][s[i]-'0'] = newnode();
now = next[now][s[i]-'0'];
}
en[now]++;
}
void build() {
queue<int>q;
fail[ro]=ro;
for(L i=0;i<2;i++){
if(next[ro][i] == -1) next[ro][i]=ro;
else {
fail[next[ro][i]]=ro;
q.push(next[ro][i]);
}
}
while(!q.empty()){
L now=q.front();q.pop();
for(L i=0;i<2;i++){
if(next[now][i]==-1)next[now][i] = next[fail[now]][i] ;
else {
fail[next[now][i]] = next[fail[now]][i];
q.push(next[now][i]);
}
}
}
}
void gao() {
inse() ;
for(int i = 1 ; i <= n ; i ++ ) {
s[i] = '0' + ((s[i] - '0') ^ 1) ;
inse();
s[i] = '0' + ((s[i] - '0') ^ 1) ;
}
build() ;
for(int i = 0 ; i <= to ; i ++ ) {
for(int j = 0 ; j <= 1 ; j ++ ) {
int v = next[i][j];
if(en[i] == 0)
a[i][v] ++ ;
}
}
for(int i = 0 ; i <= to ; i ++ ) {
if(en[i] != 0) a[i][i] += 2 ;
}
b[0] = 1 ;
for(int i = 1 ; i <= m ; i ++ ) {
memset(c, 0 , sizeof(c)) ;
for(int u = 0 ; u <= to ; u ++ ) {
for(int v = 0 ; v <= to ; v ++ ) {
c[v] += b[u] * a[u][v] ;
}
}
for(int j = 0 ; j <= to ; j ++ )
b[j] = c[j] ;
}
L ans = 0 ;
for(int i = 0 ; i <= to ; i ++ ) {
if(en[i] != 0) ans += b[i] ;
}
cout << ans << endl ;
} }ac; int main () { L t = read() ; while(t -- ) {
n = read() ; m = read() ;
scanf("%s" , s+1) ;
memset(a,0,sizeof(a)) ;
memset(b,0,sizeof(b)) ;
ac.init() ;
ac.gao() ;
}
}

  

2018 ICPC北京 H ac自动机的更多相关文章

  1. 2018 Arab Collegiate Programming Contest (ACPC 2018) E - Exciting Menus AC自动机

    E - Exciting Menus 建个AC自动机求个fail指针就好啦. #include<bits/stdc++.h> #define LL long long #define fi ...

  2. hihoCoder #1871 : Heshen's Account Book-字符串暴力模拟 自闭(getline()函数) (ACM-ICPC Asia Beijing Regional Contest 2018 Reproduction B) 2018 ICPC 北京区域赛现场赛B

    P2 : Heshen's Account Book Time Limit:1000ms Case Time Limit:1000ms Memory Limit:512MB Description H ...

  3. hihoCoder #1870 : Jin Yong’s Wukong Ranking List-闭包传递(递归) (ACM-ICPC Asia Beijing Regional Contest 2018 Reproduction A) 2018 ICPC 北京区域赛现场赛A

    P1 : Jin Yong’s Wukong Ranking List Time Limit:1000ms Case Time Limit:1000ms Memory Limit:512MB Desc ...

  4. 2016ACM/ICPC亚洲区沈阳站H - Guessing the Dice Roll HDU - 5955 ac自动机+概率dp+高斯消元

    http://acm.hdu.edu.cn/showproblem.php?pid=5955 题意:给你长度为l的n组数,每个数1-6,每次扔色子,问你每个串第一次被匹配的概率是多少 题解:先建成ac ...

  5. 2018 焦作网络赛 L Poor God Water ( AC自动机构造矩阵、BM求线性递推、手动构造矩阵、矩阵快速幂 )

    题目链接 题意 : 实际上可以转化一下题意 要求求出用三个不同元素的字符集例如 { 'A' .'B' .'C' } 构造出长度为 n 且不包含 AAA.BBB CCC.ACB BCA.CAC CBC ...

  6. 2019牛客多校八 H. How Many Schemes (AC自动机,树链剖分)

    大意: 给定树, 每条边有一个字符集合, 给定$m$个模式串, $q$个询问$(u,v)$, 对于路径$(u,v)$中的所有边, 每条边从对应字符集合中取一个字符, 得到一个串$s$, 求$s$至少包 ...

  7. 2018 ACM-ICPC 北京赛区小结 @ Reconquista

    Statistics TYPE: Onsite Contest NAME: 2018 - ICPC Regional - Asia EC - Beijing PLAT: Hihocoder TIME: ...

  8. 2016 年青岛网络赛---Family View(AC自动机)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5880 Problem Description Steam is a digital distribut ...

  9. 【HDU2825】Wireless Password (AC自动机+状压DP)

    Wireless Password Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u De ...

随机推荐

  1. Django源码安装方法及创建启动项目

    一.源码安装方法 下载源码包:https://www.djangoproject.com/download/ 输入以下命令并安装: tar xzvf Django-X.Y.tar.gz # 解压下载包 ...

  2. 解决 Invalid signature file digest for Manifest 问题

    idea打包的jar文件在spark执行是报错: Invalid signature file digest for Manifest 通过以下命令解决: zip -d myjob.jar META- ...

  3. Linux进入-adsdfsd目录

    Linux进入-adsdfsd目录 如果某个目录名称是短线(短横线)开头的 要想在命令行中进入目录,使用如下命令: cd -- -adsdfsd 延伸: 其他非寻常操作: http://www.cnb ...

  4. JavaWeb—Base64编码(转载)

    基本概念 Base64这个术语最初是在“MIME内容传输编码规范”中提出的.Base64不是一种加密算法,虽然编码后的字符串看起来有点加密的赶脚.它实际上是一种“二进制到文本”的编码方法,它能够将给定 ...

  5. centos7部署PaaS平台环境(mesos+marathon)

    假如有5台主机可以使用,ip地址如下 规划(2master+3slave) master: 192.168.248.205 ---master1 192.168.248.206 ---master2 ...

  6. PAT 1038 Recover the Smallest Number[dp][难]

    1038 Recover the Smallest Number (30 分) Given a collection of number segments, you are supposed to r ...

  7. PAT 1122 Hamiltonian Cycle[比较一般]

    1122 Hamiltonian Cycle (25 分) The "Hamilton cycle problem" is to find a simple cycle that ...

  8. mysql 提示符显示用户,数据库等信息

    命令: mysql -uroot -p --prompt="\\u@\\h:\\d \\r:\\m:\\s>" 效果: root@localhost:(mysql) 02:2 ...

  9. 有按钮的ListView

    有按钮的ListView 但是有时候,列表不光会用来做显示用,我们同样可以在在上面添加按钮.添加按钮首先要写一个有按钮的xml文件,然后自然会想到用上面的方法定义一个适配器,然后将数据映射到布局文件上 ...

  10. CSS实现文本超过指定长度显示省略号

    <style type="text/css"> li { width:200px;/*宽度,超过即会溢出*/ line-height:25px;/*设置行间距*/ te ...