题目

  点这里看题目。

分析

  首先对于模式串建立 AC 自动机,并且计算出每个状态\(p\)的贡献总和\(con(p)\)。

  考虑一个朴素的 DP :

  \(f(i,p)\):当前串长度为\(i\),匹配到\(p\)上的最大答案。

  设在\(p\)后加入字符\(c\)会转移到\(t(p,c)\), DP 的转移如下:

\[f(i+1,t(p,c))=\max\{f(i,p)+con(t(p,c))\}
\]

  如何表示这种转移? 我们可以尝试一下矩阵:

\[T_{i,j}=
\begin{cases}
con(j)&\exists c, t(i,c)=j\\
-\infty& otherwise
\end{cases}
\]

  并且可以再定义一种矩阵上的新运算 ' \(\cdot\) ' :

\[C=A\cdot B\Leftrightarrow C_{i,j}=\max\{A_{i,k}+B_{k,j}\}
\]

  那么我们对\(T\)进行\(L\)次\(T\cdot T\),再与初始向量\(\boldsymbol v\)积起来,即是答案。也就是说,答案为:

\[\boldsymbol v\cdot T^L
\]

  本质理解:

  我们的 DP 是在做什么?你会发现,我们实际上是在 AC 自动机的有向图上面做了一个从根出发走\(l\)步的最长路。

  那么\(T\)实际上是一个邻接矩阵,而 ' \(\cdot\) ' 的本质是枚举中转点计算出下一步的最长路。

  其实一次“乘法”就像是做了一次 Floyd ,我们是做了基于 Floyd 的快速幂运算!

代码

#include <cstdio>
#include <cstring> #define Tour( c ) for( int c = 0 ; c < 26 ; c ++ ) typedef long long LL; const int MAXN = 205, MAXL = 205; template<typename _T>
void read( _T &x )
{
x = 0;char s = getchar();int f = 1;
while( s > '9' || s < '0' ){if( s == '-' ) f = -1; s = getchar();}
while( s >= '0' && s <= '9' ){x = ( x << 3 ) + ( x << 1 ) + ( s - '0' ), s = getchar();}
x *= f;
} template<typename _T>
void write( _T x )
{
if( x < 0 ){ putchar( '-' ); x = ( ~ x ) + 1; }
if( 9 < x ){ write( x / 10 ); }
putchar( x % 10 + '0' );
} template<typename _T>
_T MAX( const _T a, const _T b )
{
return a > b ? a : b;
} struct matrix
{
LL mat[MAXL][MAXL];
int n, m;
matrix() { m = n = 0, memset( mat, 0xc0, sizeof mat ); }
matrix( const int N, const int M ) { n = N, m = M, memset( mat, 0xc0, sizeof mat ); }
LL* operator [] ( const int indx ) { return mat[indx]; } matrix operator * ( matrix b )
{
matrix ret = matrix( n, b.m );
for( int i = 1 ; i <= ret.n ; i ++ )
for( int j = 1 ; j <= ret.m ; j ++ )
for( int k = 1 ; k <= m ; k ++ )
ret[i][j] = MAX( ret[i][j], mat[i][k] + b[k][j] );
return ret;
} void operator *= ( matrix b ) { *this = *this * b; }
}; int ch[MAXL][26], fail[MAXL], con[MAXL], q[MAXL];
int a[MAXN];
int N, cnt; LL L;
char S[MAXL]; matrix I( const int n ) { matrix ret = matrix( n, n ); for( int i = 1 ; i <= n ; i ++ ) ret[i][i] = 0; return ret; } void insert( const int contri )
{
int p = 0, id;
for( int i = 1 ; S[i] ; i ++ )
{
id = S[i] - 'a';
if( ! ch[p][id] ) ch[p][id] = ++ cnt;
p = ch[p][id];
}
con[p] += contri;
} void init()
{
int h = 1, t = 0, u, v;
Tour( i ) if( ch[0][i] ) q[++ t] = ch[0][i];
while( h <= t )
{
u = q[h ++];
Tour( i )
{
if( v = ch[u][i] ) fail[v] = ch[fail[u]][i], q[++ t] = v;
else ch[u][i] = ch[fail[u]][i];
}
con[u] += con[fail[u]];
}
} matrix qkpow( matrix base, LL indx )
{
matrix ret = I( base.n );
while( indx )
{
if( indx & 1 ) ret *= base;
base *= base, indx >>= 1;
}
return ret;
} int main()
{
read( N ), read( L );
for( int i = 1 ; i <= N ; i ++ ) read( a[i] );
for( int i = 1 ; i <= N ; i ++ ) scanf( "%s", S + 1 ), insert( a[i] );
init();
matrix A = matrix( 1, cnt + 1 ), B = matrix( cnt + 1, cnt + 1 );
for( int p = 0 ; p <= cnt ; p ++ )
Tour( c )
B[p + 1][ch[p][c] + 1] = MAX( B[p + 1][ch[p][c] + 1], ( LL ) con[ch[p][c]] );
A[1][1] = 0;
A *= qkpow( B, L );
LL ans = 0;
for( int p = 1 ; p <= cnt + 1 ; p ++ ) ans = MAX( ans, A[1][p] );
write( ans ), putchar( '\n' );
return 0;
}

[CF696D]Legen...的更多相关文章

  1. 【CF696D】Legen...(AC自动机)(矩阵快速幂)

    题目描述 Barney was hanging out with Nora for a while and now he thinks he may have feelings for her. Ba ...

  2. CodeForces - 697F:Legen... (AC自动机+矩阵)

    Barney was hanging out with Nora for a while and now he thinks he may have feelings for her. Barney ...

  3. Codeforces 696 D. Legen...

    Description 每个字符串有些价值,问生成长度为 \(l\) 的字符串最多能获得多少价值,总字符数不超过 \(200\), \(l\leqslant 10^{14}\) . Sol AC自动机 ...

  4. Codeforces 696D Legen...(AC自动机 + 矩阵快速幂)

    题目大概说给几个字符串,每个字符串都有一个开心值,一个串如果包含一次这些字符串就加上对应的开心值,问长度n的串开心值最多可以是多少. POJ2778..复习下..太弱了都快不会做了.. 这个矩阵的乘法 ...

  5. [Codeforces 696D] Legen...

    题目大意: 给出一些匹配串,要造一个长度不超过L的字符串,每个匹配串有自己的价值,匹配串每次出现在字符串里都会贡献一次价值...要求可能得到的最大价值. 匹配串总长不超200,L<=10^14, ...

  6. 【Codeforces 696D】Legen...

    Codeforces 696 D 题意:给\(n\)个串,每个串有一个权值\(a_i\),现在要构造一个长度为\(l\leq 10^{14}\)的串,如果其中包含了第\(i\)个串,则会得到\(a_i ...

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

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

  8. codeforces泛做..

    前面说点什么.. 为了完成日常积累,傻逼呵呵的我决定来一发codeforces 挑水题 泛做.. 嗯对,就是泛做.. 主要就是把codeforces Div.1的ABCD都尝试一下吧0.0.. 挖坑0 ...

  9. html,css命名规范 (转)

    HTML+CSS命名规范总结 1.HTML部分 1.1添加必须的utf-8的字符集,并且使用HTML5的简洁 方式: <meta charset="utf-8"> 1. ...

随机推荐

  1. MVC4.0 上传文件

    Views/Import/ImportExcel.cshtml @{ Layout = null;} <!DOCTYPE html> <html><head> &l ...

  2. Canvas 画圆

    原文地址:http://hi.baidu.com/lj2tj/item/557d8d1a65adfa721009b58b --------------------------------------- ...

  3. Maven+JSP+Servlet+C3P0+Mysql实现的音乐库管理系统

    项目简介 项目来源于:https://gitee.com/sunnyandgood/OnlineMusic 本系统基于Maven+JSP+Servlet+C3P0+Mysql实现的音乐库管理系统.简单 ...

  4. CF915D Almost Acyclic Graph

    题目链接:http://codeforces.com/contest/915/problem/D 题目大意: 给出一个\(n\)个结点\(m\)条边的有向图(无自环.无重边,2 ≤ n ≤ 500, ...

  5. 28-2 类型转换函数Cast-Convet

    ------------------------类型转换函数------------------------ --cast(表达式 as 数据类型) --convert(数据类型,表达式) ' as ...

  6. Flow Control

    Exercise-01 Max Of Two Numbers Write a function that takes two numbers and returns the maximum of th ...

  7. 【新手引导】MAC如何允许安装任何来源的软件(安装第三方软件/应用)

    1 打开系统偏好设置,找到安全性与隐私(第一行第六个) 2 进入安全性与隐私,点击左下角一个锁的按钮,输入自己电脑密码,允许从以下位置下载的应用: 新电脑应该是两个选项 1.APP store 2.A ...

  8. SPL常用迭代器

    ArrayIterator 熟悉使用seek()跳过元素 熟悉使用asort,ksort排序 <?php $fruits = array( 'apple'=>'apple value', ...

  9. PHP非常用函数汇总

    1) ARRAY_FILTER — 用回调函数过滤数组中的单元 function  odd ( $var ) {      // returns whether the input integer i ...

  10. 使用容器化块存储OpenEBS在K3s中实现持久化存储

    作者简介 Giridhara Prasad,Mayadata Inc.首席工程师.在软件测试自动化.混沌工程(chaos engineering)方面有丰富的经验.目前,他正在研究开源混沌工程项目Li ...