正解:AC自动机+dp

解题报告:

传送门!

算是个比较套路的AC自动机+dp趴,,,

显然就普普通通地设状态,普普通通地转移,大概就f[i][j]:长度为i匹配到j

唯一注意的是,要加上所有子串的贡献,就在结构体中新加一个变量d表示在跟到这个节点的串以及后缀中完整串的个数

直接在bfs求fail的时候加上fail指针的d就欧克

啊当然只是为了方便描述所以说另开一个d,,,实际实现的话直接把标记结尾的那个值改成int类型搞下就欧克

然后说下细节,,,

其实就一个细节,就赋初值问题,要把除了f[i][0]的都设成-inf

原因挺显然的趴?拿样例为例好了,如果不这么设就会搞得明明只选了一个A就被计入了ABA一个串的贡献这样的乌龙QwQ

overr

#include<bits/stdc++.h>
using namespace std;
#define il inline
#define gc getchar()
#define ll long long
#define ri register int
#define rb register bool
#define rc register char
#define rp(i,x,y) for(ri i=x;i<=y;++i)
#define my(i,x,y) for(ri i=x;i>=y;--i) const int N=+,inf=1e9;
int n,K,f[N][N*],nod_cnt,as;
struct nod{int to[],fail,flg;}tr[N*];
char ch[N];
queue<int>Q; il int read()
{
rc ch=gc;ri x=;rb y=;
while(ch!='-' && (ch>'' || ch<''))ch=gc;
if(ch=='-')ch=gc,y=;
while(ch>='' && ch<='')x=(x<<)+(x<<)+(ch^''),ch=gc;
return y?x:x;
}
il void insert(char *s)
{
ri lth=strlen(s+),nw=;
rp(i,,lth)
{
if(!tr[nw].to[s[i]-'A'+])tr[nw].to[s[i]-'A'+]=++nod_cnt;
nw=tr[nw].to[s[i]-'A'+];
}
// printf("nw=%d\n",nw);
tr[nw].flg=;
}
il void bfs()
{
rp(i,,)if(tr[].to[i])Q.push(tr[].to[i]);
while(!Q.empty())
{
ri nw=Q.front();Q.pop();tr[nw].flg+=tr[tr[nw].fail].flg;//printf("nw=%d flg=%d\n",nw,tr[nw].flg);
rp(i,,)
if(tr[nw].to[i])Q.push(tr[nw].to[i]),tr[tr[nw].to[i]].fail=tr[tr[nw].fail].to[i];
else tr[nw].to[i]=tr[tr[nw].fail].to[i];
}
} int main()
{
// freopen("3041.in","r",stdin);freopen("3041.out","w",stdout);
n=read();K=read();while(n--){scanf("%s",ch+);insert(ch);}bfs();
rp(i,,K)rp(j,,nod_cnt)f[i][j]=-inf;
rp(i,,K)
{
rp(j,,nod_cnt)
{
// printf("f[%d][%d]=%d\n",i,j,f[i][j]);
rp(k,,)f[i][tr[j].to[k]]=max(f[i][tr[j].to[k]],f[i-][j]+tr[tr[j].to[k]].flg);
}
}
rp(i,,nod_cnt)as=max(as,f[K][i]);
printf("%d\n",as);
return ;
}

这儿是代码鸭QwQ

洛谷P3041 视频游戏的连击Video Game Combos [USACO12JAN] AC自动机+dp的更多相关文章

  1. [洛谷3041]视频游戏的连击Video Game Combos

    题目描述 Bessie is playing a video game! In the game, the three letters 'A', 'B', and 'C' are the only v ...

  2. 【洛谷 P3041】 [USACO12JAN]视频游戏的连击Video Game Combos(AC自动机,dp)

    题目链接 手写一下AC自动机(我可没说我之前不是手写的) Trie上dp,每个点的贡献加上所有是他后缀的串的贡献,也就是这个点到根的fail链的和. #include <cstdio> # ...

  3. [USACO12JAN]视频游戏的连击Video Game Combos(AC自动机+DP)

    Description 贝西正在打格斗游戏.游戏里只有三个按键,分别是“A”.“B”和“C”.游戏中有 N 种连击 模式,第 i 种连击模式以字符串 Si 表示,只要贝西的按键中出现了这个字符串,就算 ...

  4. P3041 [USACO12JAN]视频游戏的连击Video Game Combos

    思路 简单的AC自动机上dp,暴力跳fail向子节点直接转移即可 代码 #include <cstdio> #include <algorithm> #include < ...

  5. 【USACO12JAN】视频游戏的连击Video Game Combos

    题目描述 Bessie is playing a video game! In the game, the three letters 'A', 'B', and 'C' are the only v ...

  6. [Luogu3041][USACO12JAN]视频游戏的连击Video Game Combos

    题面 sol 设\(f_{i,j}\)表示填了前\(i\)个字母,在\(AC\)自动机上跑到了节点\(j\)的最大得分.因为匹配需要暴跳\(fail\)所以预先把\(fail\)指针上面的匹配数传下来 ...

  7. 【题解】[USACO12JAN]视频游戏的连击Video Game Combos

    好久没有写博客了,好惭愧啊……虽然这是一道弱题但还是写一下吧. 这道题目的思路应该说是很容易形成:字符串+最大值?自然联想到学过的AC自动机与DP.对于给定的字符串建立出AC自动机,dp状态dp[i] ...

  8. 洛谷 P3121 [USACO15FEB]审查(黄金)Censoring (Gold) 【AC自动机+栈】

    这个和bzoj同名题不一样,有多个匹配串 但是思路是一样的,写个AC自动机,同样是开两个栈,一个存字符,一个存当前点在trie树上的位置,然后如果到了某个匹配串的末尾,则弹栈 #include< ...

  9. 洛谷 P3041 [USACO12JAN] Video Game Combos

    题目描述 Bessie is playing a video game! In the game, the three letters 'A', 'B', and 'C' are the only v ...

随机推荐

  1. Go Revel - Cache(缓存)

    revel在服务器端提供了`cache`库用以低延迟的存储临时数据.它缓存那些需要经常访问数据库但是变化不频繁的数据,也可以实现用户会话的存储. ##有效期 一下三种方法为缓存元素设置过期时间: 1. ...

  2. hdoj:2023

    #include <iostream> #include <string> #include <vector> ][],b[],c[]; using namespa ...

  3. Fedora 21 安装 Bumblebee with the NVIDIA proprietary drivers

    最新文章:Virson's Blog 参考Fedora Wiki:http://fedoraproject.org/wiki/Bumblebee#Fedora_21

  4. Go指南练习_斐波纳契闭包

    源地址 https://tour.go-zh.org/moretypes/26 一.题目描述 让我们用函数做些好玩的事情. 实现一个 fibonacci 函数,它返回一个函数(闭包),该闭包返回一个斐 ...

  5. Spark学习笔记——在集群上运行Spark

    Spark运行的时候,采用的是主从结构,有一个节点负责中央协调, 调度各个分布式工作节点.这个中央协调节点被称为驱动器( Driver) 节点.与之对应的工作节点被称为执行器( executor) 节 ...

  6. How to set asp.net Identity cookies expires time

    If IsPersistent property of AuthenticationProperties is set to false, then the cookie expiration tim ...

  7. @Transactional(readOnly=true) in Spring

    http://www.skill-guru.com/blog/2010/12/19/transactionalreadonlytrue-in-spring/ @Transactional(readOn ...

  8. 【algorithm】 二分查找算法

    二分查找算法:<维基百科> 在计算机科学中,二分搜索(英语:binary search),也称折半搜索(英语:half-interval search)[1].对数搜索(英语:logari ...

  9. OpenGL——圆公式相关变化的绘制

    #include<iostream> #include <math.h> //旧版本 固定管线 #include<Windows.h> #include <G ...

  10. sklearn数据预处理

    一.standardization 之所以标准化的原因是,如果数据集中的某个特征的取值不服从标准的正太分布,则性能就会变得很差 ①函数scale提供了快速和简单的方法在单个数组形式的数据集上来执行标准 ...