CodeForces - 697F:Legen... (AC自动机+矩阵)
Barney was hanging out with Nora for a while and now he thinks he may have feelings for her. Barney wants to send her a cheesy text message and wants to make her as happy as possible.
Initially, happiness level of Nora is 0. Nora loves some pickup lines like "I'm falling for you" and stuff. Totally, she knows n pickup lines, each consisting only of lowercase English letters, also some of them may be equal (in writing, but different in pronouncing or meaning though). Every time Nora sees i-th pickup line as a consecutive subsequence of Barney's text message her happiness level increases by ai. These substrings may overlap, for example, Nora will see the pickup line aa twice and the pickup line ab once in text message aaab.
Due to texting app limits, Barney's text may have up to l characters.
Barney asked you to help him make Nora as much happy as possible, it's gonna be legen...
Input
The first line of input contains two integers n and l (1 ≤ n ≤ 200, 1 ≤ l ≤ 1014) — the number of pickup lines and the maximum length of Barney's text.
The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 100), meaning that Nora's happiness level increases by ai after every time seeing i-th pickup line.
The next n lines contain the pickup lines. i-th of them contains a single string siconsisting of only English lowercase letter. Summary length of all pickup lines does not exceed 200.
All strings are not empty.
Output
Print the only integer — the maximum possible value of Nora's happiness level after reading Barney's text.
Examples
3 6
3 2 1
heart
earth
art
6
3 6
3 2 8
heart
earth
art
16
题意:现在有N个字符串,每个字符串有个价值,然后叫你写一个长度为L的字符串,其价值为所含子串的价值和。
思路:AC自动机+矩阵。以前的是求方案数,用矩阵加速累加即可。关键在于如何得到最大值:把矩阵的累加改为取max。
要用-1去约束可不可以走到的位置。
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=;
int ch[maxn][],End[maxn],fail[maxn];
int num[maxn],q[maxn],head,tail,cnt;
char c[maxn];
void insert(int opt)
{
int Now=; for(int i=;c[i];i++){
if(!ch[Now][c[i]-'a']) ch[Now][c[i]-'a']=++cnt;
Now=ch[Now][c[i]-'a'];
} End[Now]+=num[opt];
}
void failbuild()
{
for(int i=;i<;i++)
if(ch[][i]) q[++head]=ch[][i];
while(tail<head){
int Now=q[++tail];
End[Now]+=End[fail[Now]];
for(int i=;i<;i++){
if(ch[Now][i]) {
q[++head]=ch[Now][i];
fail[ch[Now][i]]=ch[fail[Now]][i];
}
else ch[Now][i]=ch[fail[Now]][i];
}
}
}
struct mat
{
ll mp[maxn][maxn];
mat(){}
mat(ll x){for(int i=;i<=cnt;i++) for(int j=;j<=cnt;j++) mp[i][j]=x; }
mat friend operator *(mat a,mat b){
mat res(-);
for(int k=;k<=cnt;k++)
for(int i=;i<=cnt;i++)
for(int j=;j<=cnt;j++){
if(a.mp[i][k]!=-&&b.mp[k][j]!=-)
res.mp[i][j]=max(res.mp[i][j],a.mp[i][k]+b.mp[k][j]);
}return res;
}
mat friend operator ^(mat a,ll x){
mat res(-); res.mp[][]=; //注意起点不要设为-1
while(x){
if(x&) res=res*a;
a=a*a;x>>=;
}return res;
}
};
mat a;
void failmat()
{
for(int i=;i<=cnt;i++)
for(int j=;j<=cnt;j++)
a.mp[i][j]=-;
for(int i=;i<=cnt;i++){
for(int j=;j<;j++)
a.mp[i][ch[i][j]]=End[ch[i][j]];
}
}
int main()
{
int N,i,j; ll L,Max=;
scanf("%d%I64d",&N,&L);
for(i=;i<=N;i++) scanf("%d",&num[i]);
for(i=;i<=N;i++){
scanf("%s",c+);
insert(i);
}
failbuild();
failmat();
mat ans=a^L;
for(i=;i<=cnt;i++) Max=max(Max,ans.mp[][i]);
printf("%I64d\n",Max);
return ;
}
CodeForces - 697F:Legen... (AC自动机+矩阵)的更多相关文章
- Codeforces Round #362(Div1) D Legen...(AC自动机+矩阵快速幂)
题目大意: 给定一些开心串,每个串有一个开心值,构造一个串,每包含一次开心串就会获得一个开心值,求最大获得多少开心值. 题解: 首先先建立AC自动机.(建立fail指针的时候,对val要进行累加) 然 ...
- spoj 1676 AC自动机+矩阵快速
Text Generator Time Limit: 1386MS Memory Limit: 1572864KB 64bit IO Format: %lld & %llu Submi ...
- hdu 2243 考研路茫茫——单词情结 AC自动机 矩阵幂次求和
题目链接 题意 给定\(N\)个词根,每个长度不超过\(5\). 问长度不超过\(L(L\lt 2^{31})\),只由小写字母组成的,至少包含一个词根的单词,一共可能有多少个? 思路 状态(AC自动 ...
- 【bzoj1444】[Jsoi2009]有趣的游戏 AC自动机+矩阵乘法
题目描述 输入 注意 是0<=P 输出 样例输入 样例输出 题解 AC自动机+矩阵乘法 先将所有字符串放到AC自动机中,求出Trie图. 然后构建邻接矩阵:如果x不是某个字符串的末位置,则x连向 ...
- POJ2778 DNA Sequence(AC自动机 矩阵)
先使用AC自动机求得状态转移关系,再建立矩阵,mat[i][j]表示一步可从i到j且i,j节点均非终止字符的方案数,则此矩阵的n次方表示n步从i,到j的方法数. #include<cstdio& ...
- HDU 2243 考研路茫茫——单词情结(AC自动机+矩阵)
考研路茫茫——单词情结 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- POJ 2778 DNA Sequence(AC自动机+矩阵加速)
DNA Sequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9899 Accepted: 3717 Desc ...
- POJ2778 DNA Sequence(AC自动机+矩阵快速幂)
题目给m个病毒串,问不包含病毒串的长度n的DNA片段有几个. 感觉这题好神,看了好久的题解. 所有病毒串构造一个AC自动机,这个AC自动机可以看作一张有向图,图上的每个顶点就是Trie树上的结点,每个 ...
- HDU2243 考研路茫茫——单词情结(AC自动机+矩阵快速幂)
与POJ2778一样.这题是求长度不超过n且包含至少一个词根的单词总数. 长度不超过n的单词总数记为Sn,长度不超过n不包含词根的单词总数记为Tn. 答案就是,Sn-Tn. Sn=26+262+263 ...
随机推荐
- jQuery Validate(一)
jQuery Validate 插件为表单提供了强大的验证功能,让客户端表单验证变得更简单. 但是在学习的过程中,我也遇到了疑惑,网上的很多例子貌似都是依赖jquery.metadata.js这个库, ...
- String 的fomat方法日期转换
一.常规类型.字符类型和数值类型的格式说明符的语法如下:%[argument_index$][flags][width][.precision]conversion 可选的 argument_inde ...
- MySQL 原理性
1.MySQL的复制原理以及流程 (1).复制基本原理流程 1. 主:binlog线程——记录下所有改变了数据库数据的语句,放进master上的binlog中: 2. 从:io线程——在使用start ...
- pulsar学习笔记1:helloworld
pulsar号称是下一代的消息系统,这二年风光无限,大有干掉kafka的势头,如果想快速体验下,可以按以下步骤在本地搭建一个单机版本:(mac环境+jdk8) 一. 下载 wget https://w ...
- GS(道具,帮会)定时存储
//最近数据库存储做了重大改变,数据库内部的回头再说,先看看GS这边的 .现在感觉数据库的状态将请求包放入命令队列中,以前是全部放进去,这样让其他的数据库操作不会随着数据库定时器而变慢,GS线程去驱动 ...
- 关于 Delphi 中流的使用(7) 压缩与解压缩(TCompressionStream、TDecompressionStream)
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, For ...
- 【BZOJ2989】数列 kd-tree
[BZOJ2989]数列 Description 给定一个长度为n的正整数数列a[i]. 定义2个位置的graze值为两者位置差与数值差的和,即graze(x,y)=|x-y|+|a[x]-a[y]| ...
- Python 进程、线程、协程、锁机制,你知多少?
1.python的多线程到底有没有用? 2. 为什么在python里推荐使用多进程而不是多线程 3.进程.线程.协程.各种锁 4.Python多进程编程
- asp.net mvc 中"未找到路径“/favicon.ico”的控制器或该控制器未实现 IController。"
FavIcon.ico是一个特殊的文件,它是浏览器请求一个网站时出现的.某些浏览器在书签和收藏夹中使用这个图标.在与这些图标相关的网站被打开时,某些浏览器也在标题栏或浏览器标签中中显示这个图标. 当一 ...
- php依据地理坐标获取国家、省份、城市,及周边数据类
功能:当App获取到用户的地理坐标时,能够依据坐标知道用户当前在那个国家.省份.城市.及周边有什么数据. 原理:基于百度Geocoding API 实现.须要先注冊百度开发人员.然后申请百度AK(密钥 ...