题目

  点这里看题目。

分析

  首先对于模式串建立 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. intellij tomcat

    VMOption -server -XX:PermSize=128M -XX:MaxPermSize=256m

  2. 牛客网挑战赛19 B,C,F

    链接:https://www.nowcoder.com/acm/contest/131/B来源:牛客网 矩阵 M 包含 R 行 C 列,第 i 行第 j 列的值为 Mi,j. 请寻找一个子矩阵,使得这 ...

  3. 最小生成树 状压+prim hdu2489

    Minimal Ratio TreeTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  4. Java并发编程入门(一)

    一.为什么要并发? 出现背景:操作系统的出现,使计算机同时运行多个程序成为可能. 1.目的: 资源利用率.某些时候,程序必须等待一些外部操作完成(IO)才能继续运行,在等待时间运行其他程序,可以有效提 ...

  5. 封装 private

    封装表现: 1.方法就是一个最基本封装体. 2.类其实也是一个封装体. 从以上两点得出结论,封装的好处: 1.提高了代码的复用性. 2.隐藏了实现细节,还要对外提供可以访问的方式.便于调用者的使用.这 ...

  6. 自定义reaml创建使用实现认证

    注意清空shiro.ini 创建User对象 package cn.zys.Bean; public class User { private Integer id; private String u ...

  7. Msql 给结果拼接字符串

    SELECT CONCAT("内容:",info)AS info FROM 表名;

  8. 日期类之SimpleDateFormat

    1.System 类下的currentTimeMillis();2.Date类:java.util.Date及其子类java.sql.Date                  如何创建实例:其下的方 ...

  9. Pytorch写CNN

    用Pytorch写了两个CNN网络,数据集用的是FashionMNIST.其中CNN_1只有一个卷积层.一个全连接层,CNN_2有两个卷积层.一个全连接层,但训练完之后的准确率两者差不多,且CNN_1 ...

  10. 00016-layui 动态加载菜单 laytpl

    <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ i ...