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 ...
随机推荐
- RF --系统关键字开发
需求: 接收一个目录路径,自动遍历目录下以及子目录下的所有批处理(.bat) 文件并执行. 首先在..\Python27\Lib\site-packages 目录下创建 CustomLibrary 目 ...
- android:scrollbar的一些属性
1. activity_maim.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android ...
- Canvas中图片翻转的应用
很多时候拿到的素材都是单方向的,需要将其手动翻转来达到需求,比如下面这张图片: 它是朝右边方向的,但还需要一张朝左边方向的,于是不得不打开PS将其翻转然后做成雪碧图.如果只是一张图片还好说,但通常情况 ...
- 地上有一个m行和n列的方格。一个机器人从坐标0,0的格子开始移动,每一次只能向左,右,上,下四个方向移动一格,但是不能进入行坐标和列坐标的数位之和大于k的格子。 例如,当k为18时,机器人能够进入方格(35,37),因为3+5+3+7 = 18。但是,它不能进入方格(35,38),因为3+5+3+8 = 19。请问该机器人能够达到多少个格子?
// test20.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> #include< ...
- 巧用Excel提高工作效率
程序员如何巧用Excel提高工作效率 主要讲解下Excel中VLOOKUP函数的使用,相比于上一篇中的内容,个人觉得这个相对高级一些. 1.使用背景 为什么会使用到这个函数呢,背景是这样的,有两个系统 ...
- erlang处理mongodb日期时间格式data类型(原)
在项目中,mongo中要创建日期类型,根据这个日期类型进而对mongo设置过期时间expire,加上对应的index索引自动删除. 而mongo中的日期类型,使用ISO格式,例如:ISODate(&q ...
- 20-ab压力测试及nginx性能统计模块
一:找到apache ab模块. ab -c 1000 -n 50000 http://127.0.0.1/index.html 查看信息: 超过1024个线程 出现错误,说打开文件太多了.cket: ...
- window下Opengl与vs2012环境配置
一.opengl与C++环境配置 1. 下载opengl包. 2. 将压缩包解压后, (1)将.dll文件(GLU.DLL, GLUT.DLL, GLUT32.DLL)放到C:\Windows\Sys ...
- Oracle学习第三篇—多行函数
0 order by asc/desc 默认升序 order by 列的名字|表达式|别名|序号 把空放在后边:order by desc nulls last 1分组函数--会自动滤空值 count ...
- ios -- 极光推送《3》 pod 方法
iOS SDK 集成指南 SDK说明 适用版本 本文匹配的 SDK版本:r2.1.5 以后.查看最近更新了解最新的SDK更新情况.使用Xcode 6及以上版本可以使用新版Push SDK,Xcode ...