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

Input
3 6
3 2 1
heart
earth
art
Output
6
Input
3 6
3 2 8
heart
earth
art
Output
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自动机+矩阵)的更多相关文章

  1. Codeforces Round #362(Div1) D Legen...(AC自动机+矩阵快速幂)

    题目大意: 给定一些开心串,每个串有一个开心值,构造一个串,每包含一次开心串就会获得一个开心值,求最大获得多少开心值. 题解: 首先先建立AC自动机.(建立fail指针的时候,对val要进行累加) 然 ...

  2. spoj 1676 AC自动机+矩阵快速

    Text Generator Time Limit: 1386MS   Memory Limit: 1572864KB   64bit IO Format: %lld & %llu Submi ...

  3. hdu 2243 考研路茫茫——单词情结 AC自动机 矩阵幂次求和

    题目链接 题意 给定\(N\)个词根,每个长度不超过\(5\). 问长度不超过\(L(L\lt 2^{31})\),只由小写字母组成的,至少包含一个词根的单词,一共可能有多少个? 思路 状态(AC自动 ...

  4. 【bzoj1444】[Jsoi2009]有趣的游戏 AC自动机+矩阵乘法

    题目描述 输入 注意 是0<=P 输出 样例输入 样例输出 题解 AC自动机+矩阵乘法 先将所有字符串放到AC自动机中,求出Trie图. 然后构建邻接矩阵:如果x不是某个字符串的末位置,则x连向 ...

  5. POJ2778 DNA Sequence(AC自动机 矩阵)

    先使用AC自动机求得状态转移关系,再建立矩阵,mat[i][j]表示一步可从i到j且i,j节点均非终止字符的方案数,则此矩阵的n次方表示n步从i,到j的方法数. #include<cstdio& ...

  6. HDU 2243 考研路茫茫——单词情结(AC自动机+矩阵)

    考研路茫茫——单词情结 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  7. POJ 2778 DNA Sequence(AC自动机+矩阵加速)

    DNA Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9899   Accepted: 3717 Desc ...

  8. POJ2778 DNA Sequence(AC自动机+矩阵快速幂)

    题目给m个病毒串,问不包含病毒串的长度n的DNA片段有几个. 感觉这题好神,看了好久的题解. 所有病毒串构造一个AC自动机,这个AC自动机可以看作一张有向图,图上的每个顶点就是Trie树上的结点,每个 ...

  9. HDU2243 考研路茫茫——单词情结(AC自动机+矩阵快速幂)

    与POJ2778一样.这题是求长度不超过n且包含至少一个词根的单词总数. 长度不超过n的单词总数记为Sn,长度不超过n不包含词根的单词总数记为Tn. 答案就是,Sn-Tn. Sn=26+262+263 ...

随机推荐

  1. 笔记本中G-Sensor(加速计) M-Sensor 陀螺仪等传感器的区别

    1.G-sensor重力传感器 作用 G-sensor中文是加速度传感器的意思(英文全称是Accelerometer-sensor),它能够感知到加速力的变化,加速力就是当物体在加速过程中作用在物体上 ...

  2. Java 调用OPENOFFIC 转换文档类型

    public static void office2PDF(String sourceFile, String destFile) { try { File inputFile = new File( ...

  3. php减少损耗的方法之一 缓存对象

    即把实例后的对象缓存起来(存入变量),当需要再次实例化时,先去缓存里查看是否存在.存在则返回.否则实例化.

  4. android 怎样加速./mk snod打包

    mm命令高速编译一个模块之后,一般用adb push到手机看效果,假设环境不同意用adb push或模块不常常改.希望直接放到image里,则能够用./mk snod,这个命令只将system文件夹打 ...

  5. Go 语言从新手到大神:每个人都会踩的五十个坑(转)

    Go语言是一个简单却蕴含深意的语言.但是,即便号称是最简单的C语言,都能总结出一本<C陷阱与缺陷>,更何况Go语言呢.Go语言中的许多坑其实并不是因为Go自身的问题.一些错误你再别的语言中 ...

  6. 在diy的文件系统上创建文件的流程

    [0]README 0.1) source code are from orange's implemention of a os , and for complete code , please v ...

  7. hadoop 出现FATAL conf.Configuration: error parsing conf file,异常

    FATAL conf.Configuration: error parsing conf file: com.sun.org.apache.xerces.internal.impl.io.Malfor ...

  8. MySQL 创建自定义函数(2)

    说明:下面创建一个函数,调用自定义函数返回一个返回一个随机数. (1) 创建自定义函数

  9. delphi Base64编码/解码及数据压缩/解压知识

    一.Base64编码/解码 一般用到的是Delphi自带的单元EncdDecd,当然还有第三方提供的单元或控件,其中我所接触到的认为比较好的有Indy的TIdMimeEncode / TIdMimeD ...

  10. EasyPlayerPro Windows流媒体播放器(RTSP/RTMP/HTTP/HLS/File/TCP/RTP/UDP都能播)发布啦

    EasyPlayerPro简介 EasyPlayerPro是一款全功能的流媒体播放器,支持RTSP.RTMP.HTTP.HLS.UDP.RTP.File等多种流媒体协议播放.支持本地文件播放,支持本地 ...