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. Struts2学习一----------Struts2的工作原理及HelloWorld简单实现

    © 版权声明:本文为博主原创文章,转载请注明出处 Struts2工作原理 一个请求在Struts2框架中的处理步骤: 1.客户端初始化一个指向Servlet容器(例如Tomcat)的请求 2.这个请求 ...

  2. linux SPI驱动——gpio模拟spi驱动(三)

    一:首先在我的平台注册platform_device,保证能让spi-gpio.c能执行到probe函数. 1: struct spi_gpio_platform_data { 2: unsigned ...

  3. 通过PHP获取文件创建与修改时间

    1.获取文件创建时间示例: 1 2 $ctime=filectime("chinawinxp.txt"); echo "创建时间:".date("Y- ...

  4. 02 redis通用命令操作

    set hi hello 设置值 get hi 获取值 keys * 查询出所有的key memcached 不能查询出所有的key keys *h 模糊查找key keys h[ie] 模糊查找 k ...

  5. wpf 获取datagrid 模板列中的控件

    目前采用的 方法  (网上提供的一款) public static DataGridRow GetRow(DataGrid datagrid, int columnIndex)        {    ...

  6. going

  7. ceph pool 管理

    创建池 [root@node1 ~]# ceph osd pool create monitor pool 'monitor' created 查看池 [root@node1 ~]# ceph osd ...

  8. 内存分配器 (Memory Allocator)

    对于大多数开发人员而言,系统的内存分配就是一个黑盒子,就是几个API的调用.有你就给我,没有我就想别的办法. 来UC前,我就是这样觉得的.实际深入进去时,才发现这个领域里也是百家争鸣.非常热闹.有操作 ...

  9. (转)ARCGIS中坐标转换及地理坐标、投影坐标的定义

    原文地址:http://blog.sina.com.cn/s/blog_663d9a1f01017cyz.html 1.动态投影(ArcMap) 所谓动态投影指,ArcMap中的Data 的空间参考或 ...

  10. 《Python Machine Learning》索引

    目录部分: 第一章:赋予计算机从数据中学习的能力 第二章:训练简单的机器学习算法——分类 第三章:使用sklearn训练机器学习分类器 第四章:建立好的训练集——数据预处理 第五章:通过降维压缩数据 ...