2018 ICPC北京 H ac自动机
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自动机的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 2018 焦作网络赛 L Poor God Water ( AC自动机构造矩阵、BM求线性递推、手动构造矩阵、矩阵快速幂 )
题目链接 题意 : 实际上可以转化一下题意 要求求出用三个不同元素的字符集例如 { 'A' .'B' .'C' } 构造出长度为 n 且不包含 AAA.BBB CCC.ACB BCA.CAC CBC ...
- 2019牛客多校八 H. How Many Schemes (AC自动机,树链剖分)
大意: 给定树, 每条边有一个字符集合, 给定$m$个模式串, $q$个询问$(u,v)$, 对于路径$(u,v)$中的所有边, 每条边从对应字符集合中取一个字符, 得到一个串$s$, 求$s$至少包 ...
- 2018 ACM-ICPC 北京赛区小结 @ Reconquista
Statistics TYPE: Onsite Contest NAME: 2018 - ICPC Regional - Asia EC - Beijing PLAT: Hihocoder TIME: ...
- 2016 年青岛网络赛---Family View(AC自动机)
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5880 Problem Description Steam is a digital distribut ...
- 【HDU2825】Wireless Password (AC自动机+状压DP)
Wireless Password Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %I64d & %I64u De ...
随机推荐
- linux定时任务常用命令大全
脚本中时间戳 TIMESTAMP=`date +%Y%m%d%H%M%S`
- maven 之nexus仓库管理_私服配置
1.下载nexus私服 下载地址:http://www.sonatype.org/downloads/nexus-latest.war 2.解压 解压以下压缩包 3.配置环境变量 *\nexus-2. ...
- 005-Shell echo命令
一.概述 Shell 的 echo 指令,用于字符串的输出.命令格式: echo string 可以使用echo实现更复杂的输出格式控制. 1.显示普通字符串: echo "It is a ...
- Pytorch permute,contiguous
permute(dims),常用的维度转换方法 将tensor的维度换位 参数:dim(int)---换位顺序 >>>x = torch.randn(,,) >> ...
- Appium-Java滑动操作
Java滑动操作,通常可以直接使用API中AndroidDriver类中的swipe方法,直接进行调用 swipe(int startx, int starty, int endx, int endy ...
- (转)Spring整合Jpa
Spring-data-jpa 学习笔记(一) 作者:zeng1994 出处:http://www.cnblogs.com/zeng1994/ Spring家族越来越强大,作为一名javaWeb开发 ...
- Bootstrap3的输入框数字点击修改效果
<div class="container"><div class="page-header"><h3>Bootstrap ...
- SQL Server 创建游标(cursor)
游标的定义: 游标则是处理结果集的一种机制,它可以定位到结果集中的某一行,也可以移动游标定位到你所需要的行中进行操作数据.与 select 语句的不同是,select 语句面向的是结果集,游标面向的是 ...
- SQL Server 自定义函数(Function)——参数默认值
sql server 自定义函数分为三种类型:标量函数(Scalar Function).内嵌表值函数(Inline Function).多声明表值函数(Multi-Statement Functio ...
- 001-netty的优势
1.API简单. 2.性能高. 3.成熟稳定. 4.入门门槛低.